Wednesday 15 January 2014

c# - One Gridview with Multiple SqlDataSources -


I have a gridview that I want to use two or more separate questions with it. In other words I do not have to keep two gridviews with each with a sqldatasource.

How can I just do a gridview and use multiple SqlDataSources? For example, if I have two buttons when one is clicked on, then a datasource is used and when the second is clicked, other data sources are used while using the same gridview.

  & lt; Div id = "div1" runat = "server" & gt; & Lt; Asp: GridView id = "GridView1" runat = "server" AutoGenerateColumn = "False" cellpadding = "4" ForeColor = "# 333333" Gridlines = "none" width = "100%" Allow = "true" "True" pagesystem = "20" & gt; & Lt; AlternatingRowStyle BackColor = "White" /> & Lt; Columns & gt; & Lt; ASP: BoundfieldDefined = "MadDisk" HeaderText = "Item Details" SortXpress = "Magdisk" /> & Lt; ASP: BoundfieldDefined = "TypeDesc" header text = "Type" sortExpress = "TypeDesc" & gt; & Lt; Item Style Horizontal Align = "Center" /> & Lt; / ASP: BoundField & gt; & Lt; Asp: BoundfieldDIFF field = "Total" headerText = "total" sortExpress = "total" & gt; & Lt; Item Style Horizontal Align = "Center" /> & Lt; / ASP: BoundField & gt; & Lt; / Column & gt; & Lt; FooterStyle BackColor = "# 990000" font-bold = "true" ForeColor = "white" /> & Lt; HeaderStyle BackColor = "# 990000" font-bold = "true" ForeColor = "white" /> & Lt; PagerStyle BackColor = "# FFCC66" ForeColor = "# 333333" Horizontal Alliance = "Center" /> & Lt; RowStyle BackColor = "#FFFBD6" ForeColor = "# 333333" /> & Lt; Selected light BackAcllor = "# FFCC 66" font-bold = "true" Foreclosure = "navy" /> & Lt; Sorted aircasesllstyle backcare = "#FDF5AC" /> & Lt; Sorted HeadersStyle BackAclar = "# 4D0000" /> & Lt; Sorted folding sequestale backAcllor = "#FCF 6C0" /> & Lt; Sorted Decking Header System Back Bclar = "# 820000" /> & Lt; / ASP: GridView & gt; & Lt;% - Query to get total -% & gt; & Lt; Asp: SqlDataSource id = "SqlDataSource1" runat = "server" connectionstring = "& lt;% $ connectionSetting: TLineConnectionString%>" SelectCommand = "Select ItemsDoc, TypeDesk, Total from vwTotal where Admin = 0 Order by total DESC" & gt; & Lt; / ASP: SQLDataSource & gt; & Lt;% - Total Administrators Only Get Questions -%> & Lt; Asp: SqlDataSource id = "SqlDataSource2" runat = "server" connectionstring = "& lt;% $ connectionString: LineConnectionString% & gt;" SelectCommand = "Select ItemsDoc, TypeDesk, VWTotal from Total where Admin = 1 ORDER Total DESC" & gt; & Lt; / ASP: SQLDataSource & gt; & Lt; / Div & gt;  

On your Button Event you can change the selection command of SqlDataSource1.

  // Click button 1 SqlDataSource1. Select command = "Select itemsDada, TypeDec, Total from vwTotal where admin = 0 Total by order"; GridView1.DataSource = SqlDataSource1; GridView1.DataBind (); // Click on the button 2 SqlDataSource1.SelectCommand = "Select ItemDesc, TypeDesc, vwTotal from total where admin = 1 total order by DISE"; GridView1.DataSource = SqlDataSource1; GridView1.DataBind ();   

Edit: With dropdown list:

  Secure zero empty dropdown list 1 selected indx (object sender, EventArgs e) {int selindex = DropDownList1. SelectedIndex; If (selindex == 0) // Option 1 selected {SqlDataSource1.SelectCommand = "Select ItemsDoc, TypeDesc, Total from vwTotal where administrator = 0 total order by DESC"; GridView1.DataSource = SqlDataSource1; GridView1.DataBind (); } And if (selindex == 1) // Option 2 selected {SqlDataSource1.SelectCommand = "Select ItemsDoc, Typedisk, Total by VWTotal WHERE Admin = 1 Total Order by DSE"; GridView1.DataSource = SqlDataSource1; GridView1.DataBind (); }}   

I think you add two items to the dropdownlist collection such as [Option1 and Option2] [DropDownList1.AutoPostBack = "True"] Do not forget to make it [selected index Changed] Event

No comments:

Post a Comment