How to have mutually exclusive radiobutton controls in a datagrid.
<asp:DataGrid id=’DataGrid1′ AutoGenerateColumns=’False’ runat=’server’> <Columns> <asp:TemplateColumn HeaderText=’FirstName’> <ItemTemplate> <input type=’radio’ name=’optSelectedName’ value=<%#DataBinder.Eval(Container.DataItem, ‘FirstName’) %> onclick=’javascript:txtFname.value = this.value;’><%#DataBinder.Eval(Container.DataItem, ‘FirstName’) %> <br> </ItemTemplate> </asp:TemplateColumn> </Columns> </asp:DataGrid> <asp:Button id=’Button1′ style=’Z-INDEX: 101; LEFT: 154px; POSITION: absolute; TOP: 103px’ runat=’server’ Text=’Button’></asp:Button> <div style=’VISIBILITY: hidden’> <asp:TextBox ID=’txtFname’ Runat=’server’ /> </div> VB.NET Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ’Bind the Datagrid with dataSet End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Response.Write(‘You have selected :’ & txtFname.Text) End Sub C# private void Page_Load(object sender, System.EventArgs e) { //Bind the datagrid to dataset } private void Button1_Click(object sender, System.EventArgs e) { Response.Write(‘You have selected :’ + txtFname.Text); }
How to do Bidirectional Sorting in the DataGrid
<asp:DataGrid id=’DataGrid1′ OnSortCommand =’SortDataGrid’ runat=’server’ AllowSorting=’True’ EnableViewState=’False’></asp:DataGrid> VB.NET Protected SQLStmt As String = ‘Select * from Products ‘ Dim myconnection As SqlConnection Dim myda As SqlDataAdapter Dim ds As DataSet Dim strConn As String Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ’Put user code to initialize the page here binddata() End Sub Sub binddata() strConn = ‘Server=localhost;uid=sa;password=;database=northwind;’ myconnection = New SqlConnection(strConn) myda = New SqlDataAdapter(SQLStmt, myconnection) ds = New DataSet myda.Fill(ds, ‘AllTables’) DataGrid1.DataSource = ds DataGrid1.DataBind() End Sub Protected Sub SortDataGrid(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) If ViewState(‘SortOrder’) Is Nothing Then ViewState(‘SortOrder’) = ‘ ASC’ ElseIf ViewState(‘SortOrder’) = ‘ ASC’ Then ViewState(‘SortOrder’) = ‘ DESC’ Else ViewState(‘SortOrder’) = ‘ ASC’ End If SQLStmt = SQLStmt & ‘ ORDER BY ‘ & e.SortExpression & ‘ ‘ & ViewState(‘SortOrder’) binddata() End Sub C# SqlConnection myconnection ; SqlDataAdapter myda ; DataSet ds ; String strConn ; string SQLStmt= ‘Select * from Products ‘; private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here binddata(); } void binddata() { strConn = ‘Server=localhost;uid=sa;password=;database=northwind;’; myconnection =new SqlConnection(strConn); myda = new SqlDataAdapter(SQLStmt, myconnection); ds = new DataSet(); myda.Fill(ds, ‘AllTables’); DataGrid1.DataSource = ds; DataGrid1.DataBind(); } protected void SortDataGrid(object source , System.Web.UI.WebControls.DataGridSortCommandEventArgs e ) { if (ViewState[‘SortOrder’] ==null) { ViewState[‘SortOrder’] = ‘ ASC’; } else if (ViewState[‘SortOrder’].ToString () == ‘ ASC’ ) { ViewState[‘SortOrder’] = ‘ DESC’; } else { ViewState[‘SortOrder’] = ‘ ASC’; } SQLStmt = SQLStmt + ‘ ORDER BY ‘ + e.SortExpression.ToString () + ‘ ‘ + ViewState[‘SortOrder’]; binddata(); }
How can I have an onclick event in the DataGrid for any Column
<asp:DataGrid id=’DataGrid1′ OnItemDataBound =’ItemDB’ DataKeyField =’ProductId’ runat=’server’></asp:DataGrid> VB.NET Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ’Put user code to initialize the page here If Not Page.IsPostBack Then ’Bind the dataGrid to DataView End If End Sub Protected Sub ItemDB(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Dim dv As DataView = DataGrid1.DataSource Dim dcCol As DataColumn Dim dc As DataColumnCollection = dv.Table.Columns Dim strID As String For Each dcCol In dv.Table.Columns If e.Item.ItemType = ListItemType.AlternatingItem Or _ e.Item.ItemType = ListItemType.Item Then strID = DataGrid1.DataKeys(e.Item.ItemIndex) e.Item.Cells(dc.IndexOf(dc(dcCol.ColumnName))).Attributes.Add(‘style’, ‘cursor:hand’) e.Item.Cells(dc.IndexOf(dc(dcCol.ColumnName))).Attributes.Add(‘onclick’, _ ‘javascript:window.open(’details.aspx?id=’ & strID & ‘’,’ _ & ‘’MyPage’,’height=300,width=300’)’) End If Next End Sub C# private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here if(!Page.IsPostBack ) { //Bind Datagrid to DataView } } protected void ItemDB(object sender , System.Web.UI.WebControls.DataGridItemEventArgs e ) { DataView dv = (DataView)DataGrid1.DataSource; DataColumnCollection dc = dv.Table.Columns ; string strID; foreach (DataColumn dcCol in dv.Table.Columns) { if ((e.Item.ItemType == ListItemType.AlternatingItem )||(e.Item.ItemType == ListItemType.Item )) { strID = DataGrid1.DataKeys[e.Item.ItemIndex].ToString (); e.Item.Cells[dc.IndexOf(dc[dcCol.ColumnName])].Attributes.Add(‘style’, ‘cursor:hand’); e.Item.Cells[dc.IndexOf(dc[dcCol.ColumnName])].Attributes.Add(‘onclick’, ‘javascript:window.open(’details.aspx?id=’ + strID + ‘’,’ + ‘’MyPage’,’height=300,width=300’)’); } } }
How to display the total of a particular column at the footer of the DataGrid
<asp:DataGrid id=’DataGrid1′ OnItemDataBound=’ItemDB’ ShowFooter =true runat=’server’></asp:DataGrid> VB.NET Dim UnitPrice As Double Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ’Put user code to initialize the page here ’Bind the Data to datagrid End Sub Protected Sub UPTotal(ByVal _unitprice As Double) UnitPrice += _unitprice End Sub ’UPTotal Public Sub ItemDB(ByVal sender As Object, ByVal e As DataGridItemEventArgs) If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then UPTotal(Double.Parse(e.Item.Cells(1).Text.ToString)) Else If e.Item.ItemType = ListItemType.Footer Then e.Item.Cells(0).Text = ‘ Total ‘ e.Item.Cells(1).Text = UnitPrice.ToString() End If End If End Sub ’ItemDB C# double UnitPrice; private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here if(!Page.IsPostBack ) { //Bind the DataGrid } } protected void UPTotal(double _unitprice) { UnitPrice += _unitprice; } public void ItemDB(object sender, DataGridItemEventArgs e) { if ((e.Item.ItemType == ListItemType.Item) ||( e.Item.ItemType == ListItemType.AlternatingItem)) { UPTotal(Double.Parse ( e.Item.Cells[1].Text )); } else if (e.Item.ItemType ==ListItemType.Footer ) { e.Item.Cells [0].Text =’ Total ‘; e.Item.Cells[1].Text =UnitPrice.ToString (); } }
How to access the TemplateColumn information on SelectedIndexChanged event of a datagrid
<asp:Label id=’Label1′ runat=’server’>Label</asp:Label> <asp:DataGrid id=’DataGrid1′ OnSelectedIndexChanged=’SelectedIndexChg’ AutoGenerateColumns=’False’ runat=’server’> <Columns> <asp:ButtonColumn Text=’Select’ ButtonType=’PushButton’ CommandName=’Select’></asp:ButtonColumn> <asp:TemplateColumn HeaderText=’ProductId’> <ItemTemplate> <%#DataBinder.Eval(Container.DataItem , ‘Productid’)%> </ItemTemplate> </asp:TemplateColumn> <asp:TemplateColumn HeaderText=’ProductName’> <ItemTemplate> <%#DataBinder.Eval(Container.DataItem , ‘ProductName’)%> </ItemTemplate> </asp:TemplateColumn> </Columns> </asp:DataGrid> VB.NET Protected Sub SelectedIndexChg(ByVal sender As Object, ByVal e As System.EventArgs) Label1.Text = CType(DataGrid1.SelectedItem.Cells(1).Controls(0), DataBoundLiteralControl).Text & CType(DataGrid1.SelectedItem.Cells(2).Controls(0), DataBoundLiteralControl).Text End Sub C# protected void SelectedIndexChg(object sender, System.EventArgs e) { Label1.Text = ((DataBoundLiteralControl)DataGrid1.SelectedItem.Cells[1].Controls[0]).Text + ((DataBoundLiteralControl)DataGrid1.SelectedItem.Cells[2].Controls[0]).Text; }