【发布时间】:2014-07-01 09:41:18
【问题描述】:
我在 asp.net 工作。尝试在从 Web 服务检索到的网格视图中显示数据。我想要 gridview 中的五个字段,一个复选框字段,其他四个是来自服务的值,即
CheckboxField,FirstName,LastName,OffenseName,FineAmount
1 ) 以下是 gridview 代码,我只是将其拖放到页面中
<asp:GridView ID="GridView1" runat="server">
<HeaderStyle BackColor="#CCFF33" />
</asp:GridView>
2) 以下是我调用的方法,用于在 DataTable 中创建字段,稍后我将绑定到 gridview。
DataTable table = new DataTable();
table.Columns.Add("Select", typeof(CheckBox)); // i think problem is here
table.Columns.Add("FirstName", typeof(string));
table.Columns.Add("LastName", typeof(string));
table.Columns.Add("OffenseName", typeof(string));
table.Columns.Add("FineAmount", typeof(string));
3) 以下是使用数据填充数据表的代码
for (int i = 0; i < noOfContacts; i++)
{
object[] rowVals = new object[5];
rowVals[0] = giveCheckBox(i); // this method is declared below, which gives me a checkbox with unique id
rowVals[1] = listOfContacts[i].FirstName;
rowVals[2] = listOfContacts[i].LastName;
rowVals[3] = listOfCharges[j].GHQOffenseId;
rowVals[4] = listOfCharges[j].GHQFineAmount;
table.Rows.Add(rowVals);
}
GridView1.DataSource = table;
GridView1.DataBind();
4) 这是给我一个复选框的方法,具有唯一的 id
public CheckBox giveCheckBox(int i)
{
CheckBox chk = new CheckBox();
chk.ID = "chk_" + i;
chk.Text = "Pay";
return chk;
}
问题是当我运行程序时,它只显示四个字段,但不显示第一个复选框字段。我想显示必须的。
但是如果我在设计视图中添加复选框字段(单击 gridview 上的箭头,单击添加新字段),那么它会在绑定行引发异常,即
GridView1.DataBind();
请指导我如何使复选框字段可见。
【问题讨论】: