我正在创建一个ASP.Net网页,其中包含一个动态gridView。 用户可以通过用户名搜索gridView中的特定行;
其中一个字段包含一个复选框,我在将其加载到数据集时遇到问题。这是我的代码的一部分:
DataTable container = new DataTable();
DataRow convertedForTable = container.NewRow();
DataSet final = new DataSet();
container.Columns.Add(new DataColumn("Username", typeof(string)));
container.Columns.Add(new DataColumn("TimeJoined", typeof(DateTime)));
container.Columns.Add(new DataColumn("Email", typeof(string)));
container.Columns.Add(new DataColumn("Banned", typeof(CheckBoxField))); // Here the problem starts
container.Columns.Add(new DataColumn("Admin", typeof(CheckBoxField))); // That is also a checkboxField.
container.Columns.Add(new DataColumn("Options", typeof(object))); // And that's a GridView edit field. I have no idea what type it is.
object[] rowArray = new object[myRow.Cells.Count];
for (int i = 0; i < myRow.Cells.Count; i++)
rowArray[i] = myRow.Cells[i].Text;
convertedForTable.ItemArray = rowArray;
container.Rows.Add(convertedForTable);
final.Tables.Add(container);
Cache["Users"] = final; // From the cache, the dataset is later loaded & displayed.
无论如何,我明白了。
值类型与列类型不匹配&lt;&gt; t&lt;&gt;禁止栏目。预期的类型是CheckBoxField。
我理解错误,虽然我不知道如何解决它。我应该在这些列中添加什么类型?
答案 0 :(得分:1)
问题是您要将CheckBoxField
以外的内容存储到Banned
列
确保项目类型相同
你可以这样做:
container.Columns.Add(new DataColumn("Username", typeof(string)));
container.Columns.Add(new DataColumn("TimeJoined", typeof(DateTime)));
container.Columns.Add(new DataColumn("Email", typeof(string)));
container.Columns.Add(new DataColumn("Banned", typeof(bool)));
container.Columns.Add(new DataColumn("Admin", typeof(bool)));
container.Columns.Add(new DataColumn("Options", typeof(object)));
object[] rowArray = new object[6];
rowArray[0] = "Item 0" ;
rowArray[1] = DateTime.Now ;
rowArray[2] = "Item 2" ;
rowArray[3] = true;
rowArray[4] = false;
rowArray[5] = "object";
如果您想将CheckBox
添加到第3项和第4项,您应该执行以下操作:
container.Columns.Add(new DataColumn("Banned", typeof(CheckBox)));
container.Columns.Add(new DataColumn("Admin", typeof(CheckBox)));
rowArray[3] = new CheckBox()
{
ID = "newID3",
Checked = true// Or your condition based on myRow.Cells[3].Text
};
rowArray[4] = new CheckBox()
{
ID = "newID4",
Checked = true// Or your condition based on myRow.Cells[3].Text
};
这没有区别,您也可以将CheckBoxField
添加到Banned
和Admin
列。
希望会有所帮助。