C# - 使用 SQL 源在数据网格视图中填充组合框

本文关键字:视图 网格 填充 组合 数据网 数据 使用 SQL | 更新日期: 2023-09-27 18:31:07

我看过其他一些关于这方面的文章,但也许我做错了什么。我使用的是Visual Studio,C#,这个datagridview使用的是Visual Studio中预定义的列(未编码,使用GUI)。这甚至可能是问题的一部分,因为当我尝试加载 DGV 时,我得到了额外的列。下面是我的代码,在填充 DGV 时,我只会得到没有下拉选项的额外列。如何在专门作为下拉列表的单元格中使用下拉条目填充此 DGV 中的多行?我试图防止做一个大规模的嵌套 for 循环,以便尽可能一一地完成这些,因为我预计这个 DGV 中最多有 20K 行(我可能会通过其他方式减少,我只是还没有到达那里)。

private void ReadSQL(string query, DataGridView grid){
        try{
            string connectionString = "Server=SERVERNAME;Database=DATABASENAME;Persist Security Info=True;";
            dataAdapter = new SqlDataAdapter(query, connectionString);
            SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
            DataTable table = new DataTable();
            table.Locale = System.Globalization.CultureInfo.InvariantCulture;
            dataAdapter.Fill(table);
            bindingSource1.DataSource = table;
            //I expect something needs to happen here, not the line above.
            grid.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
            grid.RowHeadersVisible = false;
            grid.EnableHeadersVisualStyles = false;
            grid.ColumnHeadersDefaultCellStyle.BackColor = Color.DimGray;
            grid.ColumnHeadersDefaultCellStyle.ForeColor = Color.White;
            grid.GridColor = Color.RoyalBlue;
            for(int i = 0 ;  i < grid.Columns.Count; i++){
                grid.Columns[i].Width = (grid.Size.Width / grid.Columns.Count) - 1;
            }
        }catch(SqlException ex){
            MessageBox.Show("SQL ERROR: " + ex.ToString());
            MessageBox.Show(query);
        }
    }

C# - 使用 SQL 源在数据网格视图中填充组合框

这很粗糙,但它有效。我正在寻求有关如何更好地编写它的建议,即这些情况的硬编码。鲍勃叔叔会生气的!

for(int i = 0 ;  i < table.Rows.Count; i++){
    dataGridView1.Rows.Add();
    try{dataGridView1.Rows[i].Cells[0].Value  = table.Rows[i].ItemArray[0].ToString();}catch{}
    try{dataGridView1.Rows[i].Cells[1].Value  = table.Rows[i].ItemArray[1].ToString();}catch{}
    try{dataGridView1.Rows[i].Cells[2].Value  = table.Rows[i].ItemArray[2].ToString();}catch{}
    try{dataGridView1.Rows[i].Cells[3].Value  = table.Rows[i].ItemArray[3].ToString();}catch{}
    try{dataGridView1.Rows[i].Cells[4].Value  = table.Rows[i].ItemArray[4].ToString();}catch{}
    try{dataGridView1.Rows[i].Cells[5].Value  = table.Rows[i].ItemArray[5].ToString();}catch{}
    string selection1 = table.Rows[i].ItemArray[6].ToString();
    switch(selection1){
        case ""            : try{dataGridView1.Rows[i].Cells[6].Value  = (dataGridView1.Rows[i].Cells[6] as DataGridViewComboBoxCell).Items[0];}catch{}; break;
        case "Found"       : try{dataGridView1.Rows[i].Cells[6].Value  = (dataGridView1.Rows[i].Cells[6] as DataGridViewComboBoxCell).Items[1];}catch{}; break;
        case "Not Found"   : try{dataGridView1.Rows[i].Cells[6].Value  = (dataGridView1.Rows[i].Cells[6] as DataGridViewComboBoxCell).Items[2];}catch{}; break;
        case "In Progress" : try{dataGridView1.Rows[i].Cells[6].Value  = (dataGridView1.Rows[i].Cells[6] as DataGridViewComboBoxCell).Items[3];}catch{}; break;
    }
    string selection2 = table.Rows[i].ItemArray[7].ToString();
    switch(selection2){
        case ""    : try{dataGridView1.Rows[i].Cells[7].Value  = (dataGridView1.Rows[i].Cells[7] as DataGridViewComboBoxCell).Items[0];}catch{}; break;
        case "Yes" : try{dataGridView1.Rows[i].Cells[7].Value  = (dataGridView1.Rows[i].Cells[7] as DataGridViewComboBoxCell).Items[1];}catch{}; break;
        case "No"  : try{dataGridView1.Rows[i].Cells[7].Value  = (dataGridView1.Rows[i].Cells[7] as DataGridViewComboBoxCell).Items[2];}catch{}; break;
    }
    try{dataGridView1.Rows[i].Cells[8].Value  = table.Rows[i].ItemArray[8].ToString();}catch{}
    try{dataGridView1.Rows[i].Cells[9].Value  = table.Rows[i].ItemArray[9].ToString();}catch{}
    try{dataGridView1.Rows[i].Cells[10].Value = table.Rows[i].ItemArray[10].ToString();}catch{}
}