在c#中向数据表添加行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16059576/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding rows to the Datatable in c#
提问by Ashish
I am working on a project and i want to add rows to the Datatable and what is happening is if i am fetching two rows from the database only one of the two is being added to the datatable but i want both to be added to the datatable i.e the datatable is not able to hold the data of first row,the data is being overwrite by data of second row. the code of the method is. this is my calling statement.
我正在处理一个项目,我想向数据表添加行,如果我从数据库中获取两行,只有两行中的一个被添加到数据表中,但我希望两者都添加到数据表中即数据表无法保存第一行的数据,数据正在被第二行的数据覆盖。该方法的代码是。这是我的呼吁声明。
DataTable dttable = new DataTable();
dttable = gettable(dtgreater, dtcurrentdate);
public DataTable gettable(List<DateTime> objct1, DateTime objct2)
{
DataTable data=null;
for (int j = 0; j < dtgreater.Count; j++)
{
sql = "select library_issue.STUDENTCODE,library_issue.studentname,library_book.bookname,library_issue.issuedate,library_issue.returndate from library_issue join library_book on library_book.book_id = library_issue.book_id where library_issue.returndate ='" + objct1[j].ToString("dd/MM/yyyy") + "'";
ds = obj.openDataset(sql, Session["SCHOOLCODE"].ToString());
Label1.Text = (ds.Tables[0].Rows.Count).ToString();
data = new DataTable();
data.Columns.Add("STUDENTCODE", typeof(int));
data.Columns.Add("Studentname", typeof(string));
data.Columns.Add("Bookname", typeof(string));
data.Columns.Add("Issuedate", typeof(string));
data.Columns.Add("Returndate", typeof(string));
data.Columns.Add("NO of Days Exceeded", typeof(string));
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
TimeSpan ts = objct1[j] - objct2;
Label1.Text = ts.ToString("dd");
data.Rows.Add(ds.Tables[0].Rows[i]["STUDENTCODE"], ds.Tables[0].Rows[i]["studentname"], ds.Tables[0].Rows[i]["bookname"], ds.Tables[0].Rows[i]["issuedate"], ds.Tables[0].Rows[i]["returndate"], ts.ToString("dd"));
}
}
return data;
}
UPDATE:
更新:
public DataTable gettable(List<DateTime> objct1, DateTime objct2)
{
DataTable data = new DataTable();
data.Columns.Add("STUDENTCODE", typeof(int));
data.Columns.Add("Studentname", typeof(string));
data.Columns.Add("Bookname", typeof(string));
data.Columns.Add("Issuedate", typeof(string));
data.Columns.Add("Returndate", typeof(string));
data.Columns.Add("NO of Days Exceeded", typeof(string));
for (int j = 0; j < dtgreater.Count; j++)
{
sql = "select library_issue.STUDENTCODE,library_issue.studentname,library_book.bookname,library_issue.issuedate,library_issue.returndate from library_issue join library_book on library_book.book_id = library_issue.book_id where library_issue.returndate ='" + objct1[j].ToString("dd/MM/yyyy") + "'";
ds = obj.openDataset(sql, Session["SCHOOLCODE"].ToString());
Label1.Text = (ds.Tables[0].Rows.Count).ToString();
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
TimeSpan ts = objct1[j] - objct2;
Label1.Text = ts.ToString("dd");
data.Rows.Add(ds.Tables[0].Rows[i]["STUDENTCODE"], ds.Tables[0].Rows[i]["studentname"], ds.Tables[0].Rows[i]["bookname"], ds.Tables[0].Rows[i]["issuedate"], ds.Tables[0].Rows[i]["returndate"], ts.ToString("dd"));
}
}
return data;
}
Discription:As I was initializing the datatable inside the for loop which was causing the creation of new instance of datatable each time . As a result of which i was getting only one rows each time.
描述:当我在 for 循环中初始化数据表时,每次都会导致创建新的数据表实例。结果我每次只得到一排。
采纳答案by Guffa
You are creating the data table inside the loop, so in the second iteration it will discard the first data table with the first item and create a new empty one for the second item.
您正在循环内创建数据表,因此在第二次迭代中,它将丢弃带有第一项的第一个数据表,并为第二项创建一个新的空表。
Create the data table and add the columns to it before the loop:
创建数据表并在循环之前向其中添加列:
DataTable dttable = new DataTable();
dttable = gettable(dtgreater, dtcurrentdate);
public DataTable gettable(List<DateTime> objct1, DateTime objct2)
{
DataTable data = new DataTable();
data.Columns.Add("STUDENTCODE", typeof(int));
data.Columns.Add("Studentname", typeof(string));
data.Columns.Add("Bookname", typeof(string));
data.Columns.Add("Issuedate", typeof(string));
data.Columns.Add("Returndate", typeof(string));
data.Columns.Add("NO of Days Exceeded", typeof(string));
for (int j = 0; j < dtgreater.Count; j++) {
sql = "select library_issue.STUDENTCODE,library_issue.studentname,library_book.bookname,library_issue.issuedate,library_issue.returndate from library_issue join library_book on library_book.book_id = library_issue.book_id where library_issue.returndate ='" + objct1[j].ToString("dd/MM/yyyy") + "'";
ds = obj.openDataset(sql, Session["SCHOOLCODE"].ToString());
Label1.Text = (ds.Tables[0].Rows.Count).ToString();
for (int i = 0; i < ds.Tables[0].Rows.Count; i++) {
TimeSpan ts = objct1[j] - objct2;
Label1.Text = ts.ToString("dd");
data.Rows.Add(ds.Tables[0].Rows[i]["STUDENTCODE"], ds.Tables[0].Rows[i]["studentname"], ds.Tables[0].Rows[i]["bookname"], ds.Tables[0].Rows[i]["issuedate"], ds.Tables[0].Rows[i]["returndate"], ts.ToString("dd"));
}
}
return data;
}
回答by Nick N.
Place the table creation outside of the loop.
将表创建放在循环之外。
DataTable data = new DataTable();
It is probably better to use a foreachlike this:
使用这样的方法可能会更好foreach:
For example:
例如:
foreach (var dr in ds.Tables.First().Rows)
{
string studentCode = dr["STUDENTCODE"].ToString();
}
回答by Pandian
Remove the line : data = new DataTable();
删除该行: data = new DataTable();
Create the Datatable outside the For loop like below...
在 For 循环之外创建数据表,如下所示...
DataTable data = new DataTable();
data.Columns.Add("STUDENTCODE", typeof(int));
data.Columns.Add("Studentname", typeof(string));
data.Columns.Add("Bookname", typeof(string));
data.Columns.Add("Issuedate", typeof(string));
data.Columns.Add("Returndate", typeof(string));
data.Columns.Add("NO of Days Exceeded", typeof(string))
for (int j = 0; j < dtgreater.Count; j++)
{
it will solve your problem...
它会解决你的问题...
回答by Rohit
What you can do here is
你可以在这里做的是
DataTable dttable = new DataTable();
dttable = gettable(dtgreater, dtcurrentdate);
public DataTable gettable(List<DateTime> objct1, DateTime objct2)
{
DataTable data=null;
for (int j = 0; j < dtgreater.Count; j++)
{
sql = "select library_issue.STUDENTCODE,library_issue.studentname,library_book.bookname,library_issue.issuedate,library_issue.returndate from library_issue join library_book on library_book.book_id = library_issue.book_id where library_issue.returndate ='" + objct1[j].ToString("dd/MM/yyyy") + "'";
ds = obj.openDataset(sql, Session["SCHOOLCODE"].ToString());
Label1.Text = (ds.Tables[0].Rows.Count).ToString();
if(data.Columns.count==0)
{
data = new DataTable();
data.Columns.Add("STUDENTCODE", typeof(int));
data.Columns.Add("Studentname", typeof(string));
data.Columns.Add("Bookname", typeof(string));
data.Columns.Add("Issuedate", typeof(string));
data.Columns.Add("Returndate", typeof(string));
data.Columns.Add("NO of Days Exceeded", typeof(string));
}
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
TimeSpan ts = objct1[j] - objct2;
Label1.Text = ts.ToString("dd");
data.Rows.Add(ds.Tables[0].Rows[i]["STUDENTCODE"], ds.Tables[0].Rows[i]["studentname"], ds.Tables[0].Rows[i]["bookname"], ds.Tables[0].Rows[i]["issuedate"], ds.Tables[0].Rows[i]["returndate"], ts.ToString("dd"));
}
}
return data;
}
I guess this will work for you
我想这对你有用
回答by oen
DataTable dttable = new DataTable();
dttable = gettable(dtgreater, dtcurrentdate);
public DataTable gettable(List<DateTime> objct1, DateTime objct2)
{
DataTable data=null;
sql = "select library_issue.STUDENTCODE,library_issue.studentname,library_book.bookname,library_issue.issuedate,library_issue.returndate from library_issue join library_book on library_book.book_id = library_issue.book_id where library_issue.returndate ='" + objct1[j].ToString("dd/MM/yyyy") + "'";
ds = obj.openDataset(sql, Session["SCHOOLCODE"].ToString());
Label1.Text = (ds.Tables[0].Rows.Count).ToString();
for (int j = 0; j < dtgreater.Count; j++)
{
if(data.Columns.count==0)
{
data = new DataTable();
data.Columns.Add("STUDENTCODE", typeof(int));
data.Columns.Add("Studentname", typeof(string));
data.Columns.Add("Bookname", typeof(string));
data.Columns.Add("Issuedate", typeof(string));
data.Columns.Add("Returndate", typeof(string));
data.Columns.Add("NO of Days Exceeded", typeof(string));
}
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
TimeSpan ts = objct1[j] - objct2;
Label1.Text = ts.ToString("dd");
data.Rows.Add(ds.Tables[0].Rows[i]["STUDENTCODE"], ds.Tables[0].Rows[i]["studentname"], ds.Tables[0].Rows[i]["bookname"], ds.Tables[0].Rows[i]["issuedate"], ds.Tables[0].Rows[i]["returndate"], ts.ToString("dd"));
}
return data;
}
回答by Unknown
Var.GRV.Columns.Add("NAME COLUMN");
Var.GRV.Columns.Add("NAME COLUMN");
Var.GRV.Columns.Add("*");
Var.GRV.Columns.Add("*");
Var.GRV.Columns.Add("*");

