如何在按钮单击时将所选项添加到datagridview中?

时间:2014-09-08 05:02:17

标签: c# winforms datagridview

我有一个网格视图并检查了列表框。我想将选中的已检查列表框项添加为网格视图的列。

private void btnProceed_Click(object sender, EventArgs e)
        {
            if (listSubjects.Items.Count > 0)
            {
                for (int i = 0; i < listSubjects.Items.Count; i++)
                {
                    if (listSubjects.GetItemChecked(i))
                    {
                        //dataGridView1.Rows.Add(checkedListBox1.Items[i], "1");
                         DataRowView castedItem = listSubjects.Items[i] as DataRowView;
                         string item = castedItem.ToString();
                         dgvEnterMarks.Columns.Add(item, item);
                    }
                }
            }
        }

2 个答案:

答案 0 :(得分:0)

这是我做过的事情......将项目添加到集合

SelectedItems="{Binding SelectedSalesMultiCountries, Mode=TwoWay}"

将可见性绑定到属性..

<telerik:GridViewDataColumn IsVisible="{Binding CompanyNameVisibility, Mode=TwoWay}" />

现在,当选中复选框项时,将上面的属性设置为true。希望这有帮助。感谢。

答案 1 :(得分:0)

private void btnProceed_Click(object sender, EventArgs e)
{
    if (listSubjects.Items.Count > 0)
    {
       for (int i = 0; i < listSubjects.Items.Count; i++)
       {
          if (listSubjects.GetItemChecked(i))
          {
                DataRowView castedItem = listSubjects.Items[i] as DataRowView;
                string item = Convert.ToString(castedItem["SubjectName"]);
                dgvEnterMarks.Columns.Add(item, item);
          }
       }
       PopulateGridView(); 
     }
}