Hi,
I have a problem with DataTable. I have one Repeater and one GridView Controls in my web page. User gets some information from Repeater with FindControl. I don't want to store this information in database because the user can change some informations. I send these informations to a datatable. The gridview shows these informations and uses datatable as datasource.
When I use gridview with auto generated columns there is no problem, but when I try to create my columns I receive an error.
The error reads: "A field or property with the name 'urunid' was not found on the selected datasource".
the code is:
protected void rep_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "ekle")
{
.
.
.
{
DataTable dt = new DataTable();
dt = yenisepetyarat();
DataRow drow = dt.NewRow();
drow[0] = urunid;
drow[1] = yemekadi;
drow[2] = sayi;
drow[3]=porsyon;
drow[4] = ucret;
dt.Rows.Add(drow);
Session["sepet"] = dt;
DataSet ds = new DataSet();
ds.Tables.Add(dt);
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}
}
}
private DataTable yenisepetyarat()
{
DataTable dt = new DataTable();
DataColumn urunid = new DataColumn();
urunid.ColumnName = "urunid";
urunid.Caption = "Ürün İd";
urunid.DataType = typeof(int);
dt.Columns.Add(urunid);
DataColumn yemekadi = new DataColumn();
urunid.ColumnName = "yemekadi";
yemekadi.DataType = typeof(string);
dt.Columns.Add(yemekadi);
DataColumn adet = new DataColumn();
urunid.ColumnName = "adet";
adet.DataType = typeof(int);
dt.Columns.Add(adet);
DataColumn porsyon = new DataColumn();
urunid.ColumnName ="porsyon";
porsyon.DataType = typeof(string);
dt.Columns.Add(porsyon);
DataColumn fiyat = new DataColumn();
urunid.ColumnName = "fiyat";
fiyat.DataType = typeof(float);
dt.Columns.Add(fiyat);
return dt;
}
While creating datatable and adding columns to it, I check this process with "quickwatch". The problem is: I create the first column, then I create the second one. However, when the second column is created, it replaces the first column; and next to it, in the place where the second column should be I see a column created itself labelled "column1" When I continue adding 3rd and the rest of the columns this process repeats itself. Each time I add a new column it replaces the former one, and a new column appears from nowhere next to it.
dt.Columns[0].ColumnName = second column.
dt.Columns[1].ColumnName=Column1
And I get an error while showing this data on gridview.
How can I solve this problem???