使用其中的复选框动态创建gridview

时间:2012-11-21 06:39:50

标签: c# asp.net

我想创建一个动态网格视图,在该网格视图中,我想将该网格视图与列表框项绑定。我想要gridview的前两列作为复选框和 第三项作为列表框中的项目。我想要这样的东西

foreach (ListItem item in Listbox1.Items)
{
    if (item.Selected)
    {
        Listbox2.Items.Add(new ListItem(item.Text, item.Value));
    }
}

GridView gridview1 = new GridView();
foreach(ListItem item in Listbox2.Items)
{
    CheckBoxField chk = new CheckBoxField();

    chk.HeaderText = "Test1";

    gridview1.Columns.Add(chk);

    CheckBoxField chk2 = new CheckBoxField();

    chk.HeaderText = "Test2";

    gridview1.Columns.Add(chk);

    // another columns that displays the item from the list box
    // another column that displays the value of the item of the listbox, but the column is hidden.
}

我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:0)

您只需为GridView的on数据绑定事件添加事件处理程序,这意味着您可以访问该行的当前行,列和数据,然后使用任何选项将控件添加到该行(文本,已选中,已禁用)等等。)你需要。