ADO.NET DataTable / DataSet

ADO.NET DataTables and DataSets are one of the most common data structures used in ASP.NET. jqGrid supports binding to DataTable / DataSet out-of-the-box -- just make sure you set the DataSource property of the grid to the instance of the DataTable / DataSet.

Example:

<trirand:JQGrid ID="DataTableGrid" runat="server"></trirand:JQGrid>


protected DataTable GetDataTableWithSchema() { DataTable dt = new DataTable(); dt.Columns.Add("id"); dt.Columns.Add("invdate"); dt.Columns.Add("name", typeof(int)); dt.Columns.Add("amount", typeof(double)); dt.Columns.Add("tax"); dt.Columns.Add("total"); dt.Columns.Add("note"); return dt; } protected DataTable GetData() { Random r = new Random(System.DateTime.Now.Millisecond); DataTable dt = GetDataTableWithSchema(); for (int i = 0; i < 100; i++) { dt.Rows.Add(new object[] { "a" + i.ToString(), "a", i, Convert.ToDouble(i), "a", "a", "a" }); } return dt; } protected void Page_Load(object sender, EventArgs e) { DataTableGrid.DataSource = GetData(); DataTableGrid.DataBind(); }

If you are using DataSets instead of DataTable, then you can have multiple DataTables in the same DataSets. Therefore you may also need to specify the name of the DataTable to use - this can be done by using the DataMember property of the grid. If DataMember is not specified, jqGrid will use the first DataTable in the DataSet.


  Last Updated: 11/11/2009 | © Trirand, 2009