2016-01-20 68 views
1

我已經看到一些其他文章,但也許​​我做錯了什麼。我使用的是Visual Studio,C#,而這個datagridview使用的是Visual Studio中預定義的列(未編碼,使用GUI)。這甚至可能是問題的一部分,因爲當我嘗試加載DGV時,我會得到額外的列。下面是我的代碼,當填充DGV時,我只是得到沒有下拉選項的額外列。如何在具有特殊下拉的單元格中使用下拉條目將多行填充到此DGV中?我試圖阻止做一個大規模的嵌套for循環,以便儘可能一個接一個地執行這些循環,因爲我預計在這個DGV中可能會多達20K條線(我可能會通過其他方式減少,但我還沒有到那裏) 。C# - 使用SQL來源填充datagridview中的組合框

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); 
     } 
    } 
+0

看起來我正在for循環中完成此操作,這可能是有效的。當解決方案准備就緒時,我會發布此信息,而且我總是爲其他方法的想法而遊戲。 –

回答

0

這是粗俗的地獄,但它的工作原理。我對如何更好地編寫這些內容提出了建議,即這些案例的硬編碼。鮑勃叔叔會很生氣的!

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{} 
} 
+0

注意:我還必須取消數據綁定。重要! –

+0

注2:我也刪除了在Visual Studio中創建此表,並在RefreshDGV1方法中聲明瞭這些列,隨後調用了此ReadSQL方法。 –