DataGrid Error
Hi,
I am getting following error
System.Web.HttpException: DataGrid with id 'DataGrid2' could not automatically generate any columns from the selected data source
Here is my code
////////////////////////////////////////////////////////////////////
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace DRIVE.Test
{
/// <summary>
/// Summary description for DataSet.
/// </summary>
public class DataSetTest : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid2;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if(! Page.IsPostBack)
{
DataSet ds = new DataSet("MyDataSet");
DataTable dt = new DataTable("Table1");
ds.Tables.Add(dt);
DataColumn c1 = new DataColumn("id",Type.GetType("System.Int32"),"");
DataColumn c2 = new DataColumn("VendorName", Type.GetType("System.String"),"");
dt.Columns.Add(c1);
dt.Columns.Add(c2);
for(int i = 0; i <10;i++)
{
DataRow dr =dt.NewRow();
dr["id"] = i;
dr["VendorName"]= "VendorName" + i.ToString();
dt.Rows.Add(dr);
}
ds.AcceptChanges();
DataGrid1.DataSource = ds.Tables[0].DefaultView;
DataGrid1.DataBind();
//DataTable dt2 = new DataTable("Table2");
DataTable dt2 = dt.Clone();
DataRow newRow;
newRow = dt2.NewRow();
newRow["id"] = 14;
newRow["VendorName"] = 774;
//Note the alternative method for adding rows.
dt2.Rows.Add(new Object[] { 12, 555 });
dt2.Rows.Add(new Object[] { 13, 665 });
ds.Tables.Add("dt2");
ds.AcceptChanges();
DataGrid2.DataSource = ds.Tables[1].DefaultView;
DataGrid2.DataBind();
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
////////////////////////////////////////////////////////////////////
|