在数据网格视图 c# 中的特定行中添加组合框

本文关键字:添加 组合 网格 数据网 视图 数据 | 更新日期: 2023-09-27 18:37:23

我想在数据网格视图中的特定单元格中有一个组合框。 有人可以帮忙吗?对于我正在使用的数据表的表

问题:我想将组合框放在特定的单元格中,例如第 1 列和第 1 行。

以下是我填充表格的方式——

public DataTable createGridForForm(int rows, int columns)
{              
    // Create the output table.
    DataTable table = new DataTable();
    for (int i = 1; i <= columns; i++)
    {
        table.Columns.Add("column " + i.ToString());
    }
    for (int i = 1; i < rows; i++)
    {
        DataRow dr = table.NewRow();
        // populate data row with values here
        ListBox test = new ListBox();
        myTabPage.Controls.Add(test);
        table.Rows.Add(dr);
    }
    return table;
}

以下是我创建数据网格视图的方法。

private void createGridInForm(int rows, int columns)
{
    DataGridView RunTimeCreatedDataGridView = new DataGridView();
    RunTimeCreatedDataGridView.DataSource = createGridForForm(rows, columns);
    //DataGridViewColumn ID_Column = RunTimeCreatedDataGridView.Columns[0];
    //ID_Column.Width = 200;
    int positionForTable = getLocationForTable();
    RunTimeCreatedDataGridView.BackgroundColor = Color.WhiteSmoke;
    RunTimeCreatedDataGridView.Size = new Size(995, 200);
    RunTimeCreatedDataGridView.Location = new Point(5, positionForTable);
    myTabPage.Controls.Add(RunTimeCreatedDataGridView);                   
}

这是我的整个代码----

public partial class Form1 : Form
    {
        String line;
        String typeOfTable;
        int amountOfTablesInTab;

        delegate void SetComboBoxCellType(int iRowIndex);
        bool bIsComboBox = false;
        List<TabPage> tapPages = new List<TabPage>();
        TabPage myTabPage;
        DataGridView RunTimeCreatedDataGridView; 

        private const String TABS = "TABS";
        private const String TABLES = "TABLES";
        private const String GRID = "grid";
        private const String LIST = "list";
        private const int LOCATION_TABLE_TYPE = 2;
        private const int LOCATION_TABLE_AMOUNT_ROWS = 3;
        private const int LOCATION_TABLE_AMOUNT_COLUMNS = 4;
        private const int AMOUNT_COLUMNS_IN_LIST = 2;
        private const int LOCATION_TAB_FOR_TABLE = 1;
        private const int STARTING_POSITION_FOR_TABLES = 5;
        private const int TABLE_SIZE= 205;
        String[] splitTableArray;
        List<String> tabList = new List<String>();
        List<String> tablesList = new List<String>();
        public Form1()
        {
            InitializeComponent();
            //test();
           getFormContentFromFile();   
        }
        private void getFormContentFromFile()
        {
            using (StreamReader reader = new StreamReader("neo2G.res"))
            {
                while (!reader.EndOfStream)
                {
                    line = reader.ReadLine();
                    if (line.Equals(TABS))
                    {
                        while (!line.Equals(".."))
                        {
                            line = reader.ReadLine();

                            if (!line.Equals("..") && !line.Equals(""))
                            {
                                tabList.Add(line);
                            }
                        }
                    }
                    if (line.Equals(TABLES))
                    {
                        while (!line.Equals(".."))
                        {
                            line = reader.ReadLine();
                            if (!line.Equals(".."))
                            {
                                tablesList.Add(line);
                            }
                        }
                    }
                }
                createTablesInTab();
            }
        }
        private void searchTablesToMatch(int tabCount)
        {
                 int tableCount = 0;
            int tabLocationForTables;
            amountOfTablesInTab = 0;

            foreach(String table in tablesList)
            {
                splitTableLineToArray(tablesList[tableCount]);
                if (int.TryParse(splitTableArray[LOCATION_TAB_FOR_TABLE], out tabLocationForTables))
                {
                    if(tabLocationForTables == tabCount)
                    {
                        typeOfTable = splitTableArray[LOCATION_TABLE_TYPE];
                        amountOfTablesInTab++;

                        if(typeOfTable.Equals(LIST))
                        {
                            createListInForm(int.Parse(splitTableArray[LOCATION_TABLE_AMOUNT_ROWS]));
                        }
                        else if(typeOfTable.Equals(GRID))
                        {

                            createGridInForm(int.Parse(splitTableArray[LOCATION_TABLE_AMOUNT_ROWS]), int.Parse(splitTableArray[LOCATION_TABLE_AMOUNT_COLUMNS]));
                        }
                    }
                {
                }
                }
                else
                {
                    MessageBox.Show("The format for TABLES must be identical to the table format in the Configuration file format", "File Error", 
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                tableCount++;
            }
        }
        private int getLocationForTable()
         {
             int position = 0;
             for (int i = 1; i <= amountOfTablesInTab; i++)
             {
                 position = position + TABLE_SIZE;
                 if (i == 1)
                 {
                     position = STARTING_POSITION_FOR_TABLES;
                 }
             } 
           return position;
        }

        private void createGridInForm(int rows, int columns)
        {
            RunTimeCreatedDataGridView = new DataGridView();
            DataGridViewComboBoxCell c = new DataGridViewComboBoxCell();
            myTabPage.Controls.Add(RunTimeCreatedDataGridView);

            c.Items.Add("A");
            c.Items.Add("B");
            c.Items.Add("C");

            RunTimeCreatedDataGridView.DataSource = createGridForForm(rows, columns);
            //Assign it to your dataGridView
            RunTimeCreatedDataGridView.Rows[1].Cells[1] = c;

            int positionForTable = getLocationForTable();
            RunTimeCreatedDataGridView.BackgroundColor = Color.WhiteSmoke;
            RunTimeCreatedDataGridView.Size = new Size(995, 200);
            RunTimeCreatedDataGridView.Location = new Point(5, positionForTable);


        }
        private void createListInForm(int rows)
        {
            RunTimeCreatedDataGridView = new DataGridView();
            RunTimeCreatedDataGridView.DataSource = createListForForm(rows);
            int positionForTable = getLocationForTable();
            RunTimeCreatedDataGridView.BackgroundColor = Color.WhiteSmoke;

            RunTimeCreatedDataGridView.Size = new Size(995, 200);
            RunTimeCreatedDataGridView.Location = new Point(5, positionForTable);
            myTabPage.Controls.Add(RunTimeCreatedDataGridView);
        }
        private void createTablesInTab()
        {
             int countTab = 0;
            foreach (String tab in tabList)
            {
                countTab++;
                createTabInForm(tab);
                searchTablesToMatch(countTab);
               }
}
        private void createTabInForm(String tabName)
        {
            tabName = Regex.Replace(tabName, @"['d-]", string.Empty);
            tabName = tabName.Trim(':', '"');
            myTabPage = new TabPage(tabName);
            tabControl1.TabPages.Add(myTabPage);
            myTabPage.AutoScroll = true;
        }
        private void splitTableLineToArray(String tableLine)
        {
            splitTableArray = tableLine.Split(',');
        }
        public DataTable createGridForForm(int rows, int columns)
        {
            // Create the output table.
            DataTable table = new DataTable();

                   for (int i = 1; i <= columns; i++)
            {
                table.Columns.Add("column " + i.ToString());
            }

            for (int i = 1; i < rows; i++)
            {
                DataRow dr = table.NewRow();
                // populate data row with values here
                table.Rows.Add(dr);

            }
            return table;
        }

        public DataTable createListForForm(int rows)
        {
            // Create the output table.
            DataTable table = new DataTable();

            for (int i = 1; i <= AMOUNT_COLUMNS_IN_LIST ; i++)
            {
                table.Columns.Add("column " + i.ToString());
            }
            for (int i = 1; i < rows; i++)
            {
                DataRow dr = table.NewRow();
                // populate data row with values here
                table.Rows.Add(dr);

               }
            return table;
        }
    } 

在数据网格视图 c# 中的特定行中添加组合框

首先必须使用 DataGridViewComboBoxCell 类创建"组合框"

此类具有与原始组合框非常相似的属性

试试这个

//Create comboBox
DataGridViewComboBoxCell c = new DataGridViewComboBoxCell();
c.Items.Add("A");
c.Items.Add("B");
c.Items.Add("C");
//Assign it to your dataGridView
yourDataGridView.Rows[rowNum].Cells[colNum] = c;

您可以将 RowDatabound 事件侦听器添加到网格视图中,并且可以控制要实现的逻辑。

您可以添加如下事件侦听器

RunTimeCreatedDataGridView.RowDataBound += new GridViewRowEventHandler(RunTimeCreatedDataGridView_RowDataBound);

事件处理程序将是受保护的无效RunTimeCreatedDataGridView_RowDataBound(对象发送器,GridViewRowEventArgs e) {"要显示的逻辑"复选框}

我找到了答案

 //create columns as textbox columns(or whatever you want)
            DataGridViewTextBoxColumn dgc = new DataGridViewTextBoxColumn();
            DataGridViewTextBoxColumn dgc1 = new DataGridViewTextBoxColumn();
            //add the colums to the table
            dataGridView1.Columns.Add(dgc);
            dataGridView1.Columns.Add(dgc1);
            //set header text here if you wish
            //create dgv combobox cells and add items to the collection
            DataGridViewComboBoxCell cbc = new DataGridViewComboBoxCell();
            cbc.Items.Add("item 1");
            cbc.Items.Add("item 2");
            DataGridViewComboBoxCell cbc1 = new DataGridViewComboBoxCell();
            cbc1.Items.Add("item 1");
            cbc1.Items.Add("item 2");
            //create 2 rows here so that you can insert future columns without having them go above your comboboxes
            dataGridView1.Rows.Add(2);
            //set row one cells to combobox cells
            dataGridView1.Rows[0].Cells[0] = cbc;
            dataGridView1.Rows[0].Cells[1] = cbc1;