c#添加一个带有dataGridViewComboBoxCell的dgv行 [英] c# add a dgv row with a dataGridViewComboBoxCell

查看:133
本文介绍了c#添加一个带有dataGridViewComboBoxCell的dgv行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在DGV中有5列:复选框,字符串,字符串,组合框,组合框。



两个combobox列都配置为datagridviewcomboboxcolumns(通过VisualStudio设计器)。我的问题是添加行。



我目前的尝试是:列已经定义,我通过dataGridView.Rows.Add添加行。
为此,我使用一组对象。示例:

  dataGridViewRow row = new dataGridViewRow(); 
object [] obj = new object [5] {true,str1,str2,null,null};
dataGridView1.Rows.Add(obj);

通过没有任何错误。但是在逻辑上,组合框没有填充任何东西。



我尝试将数据源设置为一行的第4和第5个单元格:



错误...使用ROW.dataGridViewComboBoxCell.Items.Add:项目不显示...



fill obj [3] and 4 with一个新的DGVcomboBoxCell或-Column:

 错误...:错误消息说dataGridViewComboBoxCell-Value无效

更多信息:每列应该在comboBox中具有相同的项目(以前通过互联网加载,如xml)。将dataSource设置为两列可以销毁整个DGV(我认为是因为其他colmns没有Datasource)
简而言之:如何将Rows添加到包含组合框的DGV中?



真诚,
NoMad



编辑:这里有一些代码来解决我的问题: p>

  DataGridViewCheckBoxColumn check = new DataGridViewChe ckBoxColumn(); 
check.Name =Col1;
dataGridView1.Columns.Add(check);

dataGridView1.ColumnCount = 3;
dataGridView1.Columns [1] .Name =Col2;
dataGridView1.Columns [2] .Name =Col3;

object [] row = new object [] {true,str1,str2};
dataGridView1.Rows.Add(row);

DataGridViewComboBoxColumn combo1 = new DataGridViewComboBoxColumn();
DataGridViewComboBoxColumn combo2 = new DataGridViewComboBoxColumn();

combo1.Name =Col4;
combo1.Items.Add(100x100);
combo1.Items.Add(200x200);

combo2.Name =Col5;
combo2.Items.Add(option1);
combo2.Items.Add(option2);

dataGridView1.Columns.Add(combo1);
dataGridView1.Columns.Add(combo2);

首先添加一行,转换列,配置它们并将其添加到行中。
否需要在设计器中指定列。

解决方案

您可以使用此解决方案将项目添加到datagird查看combobox列

  DataSet ds; // class variable 
public Form1()
{
InitializeComponent();

ds = new DataSet();
//列1(普通textColumn):
dataGridView1.Columns.Add(col1,Column1);
//列2(comboBox):
DataGridViewComboBoxColumn comboCol = new DataGridViewComboBoxColumn();
comboCol.Name =cmbColumn;
comboCol.HeaderText =combobox column;
dataGridView1.Columns.Add(comboCol);

//为每个数据源使用dataTable:
(int i = 0; i< 10; i ++)
{
string text =item+一世; //文本数据
int [] data = {1 * i,2 * i,3 * i}; //数据comboBox:

//创建新的dataTable:
DataTable表=新的DataTable(表+ i);
table.Columns.Add(column1,typeof(string));

// fillig rows:
foreach(数据中的int项)
table.Rows.Add(item);

//将表添加到dataSet:
ds.Tables.Add(table);

//在dgv(文本和组合框)中创建新行:
CreateCustomComboBoxDataSouce(i,text,table);
}
}

private void CreateCustomComboBoxDataSouce(int row,string texst,DataTable table)//行索引和两个参数
{
dataGridView1。 Rows.Add(texst);
DataGridViewComboBoxCell comboCell = dataGridView1 [1,row]作为DataGridViewComboBoxCell;
comboCell.DataSource = new BindingSource(table,null);
comboCell.DisplayMember =column1; //列名称indataTable显示!
comboCell.ValueMember =column1; // vlaue如果需要
//(主要是你使用这两个属性,如:名称为DisplayMember,Id为ValueMember)
}

如果上面没有工作,请查看下面的解决方案。



你可以通过DataGridViewComboBoxCell来实现。根据单元格的rowIndex,将不同的数据源(string [])设置为不同的DataGridViewComboBoxCell。编码如下:

  private void dataGridView1_CellClick(object sender,DataGridViewCellEventArgs e)
{
if(e .ColumnIndex == 0)
{
DataGridViewComboBoxCell combo = this.dataGridView1 [0,e.RowIndex]作为DataGridViewComboBoxCell;

if(e.RowIndex == 0)
{
//这些数据将显示在comboBox中:
string [] data = {item A1 ,项目B1,项目C1};
combo.DataSource = data;
}
if(e.RowIndex == 1)
{
//这些数据将显示在comboBox中:
string [] data = {item A2 ,项目B2,项目C2};
combo.DataSource = data;
}
if(e.RowIndex == 2)
{
//这些数据将显示在comboBox中:
string [] data = {item A3 ,项目B3,项目C3};
combo.DataSource = data;
}

}
}
}


I'm currently trying to add a ComboBox to a dataGridView.

In the DGV, there are 5 columns: checkbox, string, string, combobox, combobox.

both combobox-columns are configured as datagridviewcomboboxcolumns (via VisualStudio designer). My problem is to add rows.

My current try is: the columns are already defined and I add rows via dataGridView.Rows.Add. For that, I'm using an array of objects. Example:

dataGridViewRow row = new dataGridViewRow();
object[] obj = new object[5] {true, "str1", "str2", null, null};
dataGridView1.Rows.Add(obj);

This passes without any errors. But logically, the comboBoxes aren't filled with anything.

I tried setting a datasource to the 4th and 5th cell of a row:

Error...Using ROW.dataGridViewComboBoxCell.Items.Add: Items are not displayed...

filling obj[3] and 4 with a new DGVcomboBoxCell or -Column:

 Error... :The error message says "The dataGridViewComboBoxCell-Value is invalid.

Further information: Each column should have the same Items in the comboBoxes. (These are previously loaded via internet, as xml). Setting a dataSource to the two columns destroys the whole DGV (I think because the other colmns don't have a Datasource). In a nutshell: How to add Rows to a DGV which contain comboboxes filled with items?

Sincerely, NoMad

edit: here's some code to solve my problem:

        DataGridViewCheckBoxColumn check = new DataGridViewCheckBoxColumn();
        check.Name = "Col1";
        dataGridView1.Columns.Add(check);

        dataGridView1.ColumnCount = 3;
        dataGridView1.Columns[1].Name = "Col2";
        dataGridView1.Columns[2].Name = "Col3";

        object[] row = new object[] { true, "str1", "str2" };
        dataGridView1.Rows.Add(row);

        DataGridViewComboBoxColumn combo1 = new DataGridViewComboBoxColumn();
        DataGridViewComboBoxColumn combo2 = new DataGridViewComboBoxColumn();

        combo1.Name = "Col4";
        combo1.Items.Add("100x100");
        combo1.Items.Add("200x200");

        combo2.Name = "Col5";
        combo2.Items.Add("option1");
        combo2.Items.Add("option2");

        dataGridView1.Columns.Add(combo1);
        dataGridView1.Columns.Add(combo2); 

First add a row, cast columns, configure them and add them to the row. No Columns need to be previously specified in the designer.

解决方案

You can use this solution for adding items to datagird view combobox column

 DataSet ds; //class variable
    public Form1()
    {
        InitializeComponent();

        ds = new DataSet();
        //column 1 (normal textColumn):
        dataGridView1.Columns.Add("col1", "Column1");
        //column 2 (comboBox):
        DataGridViewComboBoxColumn comboCol = new DataGridViewComboBoxColumn();
        comboCol.Name = "cmbColumn";
        comboCol.HeaderText = "combobox column";
        dataGridView1.Columns.Add(comboCol);

        //using dataTable for each datasource:             
        for (int i = 0; i < 10; i++)
        {
            string text = "item " + i; //data for text
            int[] data = { 1 * i, 2 * i, 3 * i }; //data for comboBox:

            //create new dataTable:
            DataTable table = new DataTable("table" + i);
            table.Columns.Add("column1", typeof(string));

            //fillig rows:
            foreach (int item in data)
                table.Rows.Add(item);

            //add table to dataSet:
            ds.Tables.Add(table);

            //creating new row in dgv (text and comboBox):
            CreateCustomComboBoxDataSouce(i, text, table);
        }
    }

    private void CreateCustomComboBoxDataSouce(int row, string texst, DataTable table) //row index ,and two parameters
    {
        dataGridView1.Rows.Add(texst);
        DataGridViewComboBoxCell comboCell = dataGridView1[1, row] as DataGridViewComboBoxCell;
        comboCell.DataSource = new BindingSource(table, null);
        comboCell.DisplayMember = "column1"; //name of column indataTable to display!!
        comboCell.ValueMember = "column1"; // vlaue if needed 
        //(mostly you used these two propertes like: Name as DisplayMember, and Id as ValueMember)
    }

if the above is not working take a look at the solution below..

you can do it via DataGridViewComboBoxCell. According to the cell's rowIndex, set different datasource(string[]) to the different DataGridViewComboBoxCell. Coding like this:

    private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        if (e.ColumnIndex == 0)
        {
            DataGridViewComboBoxCell combo = this.dataGridView1[0, e.RowIndex] as DataGridViewComboBoxCell;

                if (e.RowIndex == 0)
                {
                    //these data will be displayed in comboBox:
                     string[] data= {"item A1", "item B1", "item C1"};  
                    combo.DataSource = data;
                }
                if (e.RowIndex == 1)
                {
                    //these data will be displayed in comboBox:
                    string[] data = {"item A2", "item B2", "item C2"};                        
                    combo.DataSource = data;
                }
                if (e.RowIndex == 2)
                {
                    //these data will be displayed in comboBox:
                    string[] data = { "item A3", "item B3", "item C3" };                           
                    combo.DataSource = data;
                }

            }
        }
    }    

这篇关于c#添加一个带有dataGridViewComboBoxCell的dgv行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆