将数据表值逐行添加到gridview中 [英] add datatable value to gridview row by row problem
本文介绍了将数据表值逐行添加到gridview中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨朋友们,
这是我的代码,用于逐行向gridview添加数据表值。在这里,我无法在asp.net中为databind创建新行。如何解决它。
Hi friends,
This is my code for adding datatable value to gridview row by row. Here I cannot create new row for databind in asp.net. how can solve it.
DataTable bindclassattendance = inter.getstudentattendresult(comp);
if (bindclassattendance.Rows.Count>0 )
{
TemplateField amtemp = new TemplateField();
amtemp.ShowHeader = true;
amtemp.HeaderText = "AM";
amtemp.ItemTemplate = new gridviewtemplate(DataControlRowType.DataRow, "AM", "AM", "CheckBox");
Classattendancegrid.Columns.Add(amtemp);
TemplateField pmtemp = new TemplateField();
pmtemp.ShowHeader = true;
pmtemp.HeaderText = "PM";
pmtemp.ItemTemplate = new gridviewtemplate(DataControlRowType.DataRow, "PM", "PM", "CheckBox");
Classattendancegrid.Columns.Add(pmtemp);
//Classattendancegrid.DataSource = bindclassattendance;
// Classattendancegrid.DataBind();
for (int i = 0; i < bindclassattendance.Rows.Count; i++)
{
Classattendancegrid.Rows[i].Cells[0].Text = bindclassattendance.Rows[i]["AdmissionNumber"].ToString();
Classattendancegrid.Rows[i].Cells[1].Text = bindclassattendance.Rows[i]["RollNumber"].ToString();
Classattendancegrid.Rows[i].Cells[2].Text = bindclassattendance.Rows[i]["Name"].ToString();
bool check1 = Convert.ToBoolean(bindclassattendance.Rows[i]["AM"].ToString());
if (check1)
{
CheckBox chk = Classattendancegrid.Rows[i].FindControl("AM") as CheckBox;
chk.Checked = true;
}
else
{
CheckBox chk = Classattendancegrid.Rows[i].FindControl("AM") as CheckBox;
chk.Checked = false;
}
bool check2 = Convert.ToBoolean(bindclassattendance.Rows[i]["PM"].ToString());
if (check2)
{
CheckBox chk = Classattendancegrid.Rows[i].FindControl("PM") as CheckBox;
chk.Checked = true;
}
else
{
CheckBox chk = Classattendancegrid.Rows[i].FindControl("PM") as CheckBox;
chk.Checked = false;
}
}
推荐答案
而不是添加行手动为什么不使用手动添加的其他列将数据表直接绑定到gridview的数据源?
我更愿意在数据表中添加其他列&将使用aspx页面本身的项目模板列添加复选框,而不是在.cs页面上写入
Rather than adding rows manually why don't you bind the datatable directly to the datasource of the gridview with additional columns you are adding manually?
I would prefer to add additional columns in the datatable & will add checkbox using the item template column in the aspx page itself rather than writing on .cs page
这篇关于将数据表值逐行添加到gridview中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文