using System; using System.Data; using System.Data.OleDb; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; publicpartialclass Exam_GridViewNested : System.Web.UI.Page ...{ string ConnectionString =@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|aspxWeb.mdb;Persist Security Info=True"; OleDbConnection cn1; protectedvoid Page_Load(object sender, EventArgs e) ...{ if (!Page.IsPostBack) ...{ OleDbConnection cn =new OleDbConnection(ConnectionString); cn.Open(); cn1 =new OleDbConnection(ConnectionString); cn1.Open(); OleDbCommand cmd =new OleDbCommand("select * from [Subject]", cn); OleDbDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); MasterGridView.DataSource = dr; MasterGridView.DataBind(); dr.Close(); cmd.Dispose(); cn.Dispose(); cn1.Dispose(); cn = cn1 =null; } } protectedvoid MasterGridView_RowDataBound(object sender, GridViewRowEventArgs e) ...{ if (e.Row.RowType == DataControlRowType.DataRow) ...{ GridView oGridView = (GridView)e.Row.FindControl("DetailGridView"); if (oGridView !=null) ...{ OleDbCommand cmd =new OleDbCommand("select top 10 * from Document Where pid = "+ MasterGridView.DataKeys[e.Row.RowIndex].Value, cn1); OleDbDataReader dr1 = cmd.ExecuteReader(); oGridView.DataSource = dr1; oGridView.DataBind(); dr1.Close(); cmd.Dispose(); } } } }
VB.NET
Private ConnectionString AsString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|aspxWeb.mdb;Persist Security Info=True" Private cn1 As OleDbConnection
ProtectedSub Page_Load(ByVal sender AsObject, ByVal e As EventArgs) IfNot Page.IsPostBack Then Dim cn As OleDbConnection =New OleDbConnection(ConnectionString) cn.Open cn1 =New OleDbConnection(ConnectionString) cn1.Open Dim cmd As OleDbCommand =New OleDbCommand("select * from [Subject]", cn) Dim dr As OleDbDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection) MasterGridView.DataSource = dr MasterGridView.DataBind dr.Close cmd.Dispose cn.Dispose cn1.Dispose cn = cn1 =Nothing EndIf End Sub
ProtectedSub MasterGridView_RowDataBound(ByVal sender AsObject, ByVal e As GridViewRowEventArgs) If e.Row.RowType = DataControlRowType.DataRow Then Dim oGridView As GridView =CType(e.Row.FindControl("DetailGridView"), GridView) IfNot (oGridView IsNothing) Then Dim cmd As OleDbCommand =New OleDbCommand("select top 10 * from Document Where pid = "+ MasterGridView.DataKeys(e.Row.RowIndex).Value, cn1) Dim dr1 As OleDbDataReader = cmd.ExecuteReader oGridView.DataSource = dr1 oGridView.DataBind dr1.Close cmd.Dispose EndIf EndIf End Sub