How to display a Master Detail data using DataGrid

<asp:datagrid id=’DataGrid1′ ShowHeader =false AutoGenerateColumns =true runat=’server’ DataKeyField=’OrderId’ OnItemDataBound=’ItemDB’> <Columns> <asp:TemplateColumn > <ItemTemplate> <b><%#DataBinder.Eval(Container.DataItem,’OrderId’)%></b> </ItemTemplate> </asp:TemplateColumn> </Columns> </asp:datagrid> VB.NET Dim cn As SqlConnection Dim da As SqlDataAdapter Dim ds As DataSet 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 cn = New SqlConnection(‘Server=localhost;uid=sa;pwd=;database=northwind’) If Not Page.IsPostBack Then da = New SqlDataAdapter(‘SELECT orderid FROM orders’, cn) ds = New DataSet da.Fill(ds, ‘Orders’) DataGrid1.DataSource = ds DataGrid1.DataBind() End If End Sub ’Page_Load Protected Sub ItemDB(ByVal sender As Object, ByVal e As DataGridItemEventArgs) If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then Dim dgDetails As New DataGrid Dim orderid As Integer = CInt(CType(e.Item.DataItem, DataRowView)(‘OrderID’)) dgDetails.DataSource = GetOrderDetails(orderid) dgDetails.DataBind() e.Item.Cells(1).Controls.Add(dgDetails) End If End Sub ’ItemDB Function GetOrderDetails(ByVal id As Integer) As DataSet da = New SqlDataAdapter(‘SELECT * FROM [Order Details] where orderid=’ + id.ToString, cn) ds = New DataSet da.Fill(ds, ‘OrderDetails’) Return ds End Function ’GetOrderDetails C# SqlConnection cn; SqlDataAdapter da; DataSet ds; private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here cn= new SqlConnection (‘Server=localhost;uid=sa;pwd=;database=northwind’); if (!Page.IsPostBack) { da= new SqlDataAdapter (‘SELECT orderid FROM orders ‘, cn); ds= new DataSet (); da.Fill (ds, ‘Orders’); DataGrid1.DataSource = ds; DataGrid1.DataBind (); } } protected void ItemDB(Object sender,DataGridItemEventArgs e ) { if ((e.Item.ItemType == ListItemType.Item)||(e.Item.ItemType == ListItemType.AlternatingItem )) { DataGrid dgDetails = new DataGrid(); int orderid =(int) ((DataRowView)e.Item.DataItem)[‘OrderID’] ; dgDetails.DataSource = GetOrderDetails(orderid ); dgDetails.DataBind(); e.Item.Cells[1].Controls.Add(dgDetails); } } DataSet GetOrderDetails(int id ) { da= new SqlDataAdapter (‘SELECT * FROM [Order Details] where orderid= ‘ + id, cn); ds= new DataSet (); da.Fill (ds, ‘OrderDetails’); return ds; }

How to select a record in the datagrid and start editing/updating the record using textboxes

<asp:DataGrid id=’DataGrid1′ OnItemCommand=’ItemCommand’ style=’Z-INDEX: 101; LEFT: 15px; POSITION: absolute; TOP: 23px’ runat=’server’ > <Columns> <asp:ButtonColumn Text=’Edit’ ButtonType=’PushButton’ CommandName=’Edit’> </asp:ButtonColumn> </Columns> </asp:DataGrid> <asp:TextBox id=’txtRegionID’ style=’Z-INDEX: 102; LEFT: 352px; POSITION: absolute; TOP: 24px’ runat=’server’></asp:TextBox> <asp:TextBox id=’txtRegionDescription’ style=’Z-INDEX: 103; LEFT: 352px; POSITION: absolute; TOP: 64px’ runat=’server’></asp:TextBox> <asp:Button id=’btnUpdate’ style=’Z-INDEX: 104; LEFT: 304px; POSITION: absolute; TOP: 128px’ runat=’server’ Text=’Update’></asp:Button> <asp:Label id=’lblMessage’ style=’Z-INDEX: 105; LEFT: 296px; POSITION: absolute; TOP: 176px’ runat=’server’></asp:Label> VB.NET Dim cn As SqlConnection Dim da As SqlDataAdapter Dim cmd As SqlCommand Dim strsql As String Dim ds As DataSet 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 cn = New SqlConnection(‘Server=localhost;uid=sa;pwd=;database=northwind;’) If Not Page.IsPostBack Then BindData() End If End Sub Sub BindData() DataGrid1.DataSource = GetData(‘Select * from Region’) DataGrid1.DataBind() End Sub Function GetData(ByVal strSql As String) As DataSet da = New SqlDataAdapter(strSql, cn) ds = New DataSet() da.Fill(ds) Return ds End Function Protected Sub ItemCommand(ByVal source As Object, _ ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) If e.CommandName = ‘Edit’ Then ’Fill the Textboxes with relevant data FillTheData(e.Item.Cells(1).Text, e.Item.Cells(2).Text) End If End Sub Sub FillTheData(ByVal RegionID As String, ByVal RegionDescription As String) txtRegionID.Text = RegionID txtRegionDescription.Text = RegionDescription End Sub Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click Try strsql = ‘Update Region set RegionDescription=@RegionDescription where RegionId=@RegionId’ cmd = New SqlCommand(strsql, cn) cmd.Parameters.Add(New SqlParameter(‘@RegionId’, SqlDbType.Int)) cmd.Parameters.Add(New SqlParameter(‘@RegionDescription’, SqlDbType.NVarChar, 40)) cmd.Parameters(‘@RegionId’).Value = Convert.ToInt32(txtRegionID.Text) cmd.Parameters(‘@RegionDescription’).Value = txtRegionDescription.Text cn.Open() cmd.ExecuteNonQuery() BindData() lblMessage.Text = ‘Updated Successfully’ Catch ex As Exception lblMessage.Text = ex.Message lblMessage.ForeColor = Color.Red Finally cn.Close() End Try End Sub C# SqlConnection cn; SqlDataAdapter da ; SqlCommand cmd ; string strsql ; DataSet ds ; private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here cn = new SqlConnection(‘Server=localhost;uid=sa;pwd=;database=northwind;’); if(!Page.IsPostBack) { //Code to Bind the data to the Datagrid BindData(); } } void BindData() { DataGrid1.DataSource = GetData(‘Select * from Region’); DataGrid1.DataBind(); } DataSet GetData(string strSql) { da = new SqlDataAdapter(strSql, cn); ds = new DataSet(); da.Fill(ds); return ds; } protected void ItemCommand(Object source,System.Web.UI.WebControls.DataGridCommandEventArgs e) { if (e.CommandName == ‘Edit’) { //’Fill the Textboxes with relevant data FillTheData(e.Item.Cells[1].Text, e.Item.Cells[2].Text); lblMessage.Text=”; } } void FillTheData(string RegionID,string RegionDescription) { txtRegionID.Text = RegionID; txtRegionDescription.Text = RegionDescription; } private void btnUpdate_Click(object sender, System.EventArgs e) { try { strsql = ‘Update Region set RegionDescription=@RegionDescription where RegionId=@RegionId’; cmd = new SqlCommand(strsql, cn); cmd.Parameters.Add(new SqlParameter(‘@RegionId’, SqlDbType.Int)); cmd.Parameters.Add(new SqlParameter(‘@RegionDescription’, SqlDbType.NVarChar, 40)); cmd.Parameters[‘@RegionId’].Value = Convert.ToInt32(txtRegionID.Text); cmd.Parameters[‘@RegionDescription’].Value = txtRegionDescription.Text; cn.Open(); cmd.ExecuteNonQuery(); BindData(); lblMessage.Text = ‘Updated Successfully’; } catch (Exception ex) { lblMessage.Text = ex.Message; lblMessage.ForeColor = Color.Red; } finally { cn.Close(); } }