在datagrid视图c#中的特定行中添加一个组合框

时间:2015-01-13 10:59:56

标签: c# datagridview

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

问题:我想在第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;
}

以下是我创建datagridview的方法。

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;
        }

    } 

3 个答案:

答案 0 :(得分:1)

您必须先使用 DataGridViewComboBoxCell

创建'组合框'

此类与原始comboBox具有非常相似的属性

试试这个

//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;

答案 1 :(得分:0)

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

您可以添加如下所示的事件监听器

RunTimeCreatedDataGridView.RowDataBound += new GridViewRowEventHandler(RunTimeCreatedDataGridView_RowDataBound);

事件处理程序将是 protected void RunTimeCreatedDataGridView_RowDataBound(object sender,GridViewRowEventArgs e)     { //显示逻辑复选框 }

答案 2 :(得分:0)

我找到了答案

 //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;