2017-10-10 62 views
0

我正在處理這個小小的C#項目,我需要將數據添加到DataGridView控件。我曾經工作過,但很遺憾,因爲我對代碼做了一些修改,我不記得以前是怎麼做的。我記得的是,在VisualStudio中,我會回到Form.Designer.cs,並在測試之前將一些控件更改爲public static,以便我可以添加行。我在網上閱讀了一些關於它的內容,他們建議不要這樣做,這就是爲什麼我改變了我的代碼。無法添加行C#DataGridView

總之向前走,我有有方法的主類AutoCheck.cs

public void addNASDestination(string[] info){ 
     /*string[0] = Name 
     * string[1] = Path 
     * string[2] = Username 
     * string[3] = Password - Needs to be passed to XML encrypted. Not displayed in the table at all 
     */ 
     destinationsTable.Rows.Add(info[0], "NAS", info[1], info[2], info[3]); 
     destinationsTable.Update(); 
     destinationsTable.Refresh(); 
     checkTableRowCount(); 
    } 

public void addBDRDestination(string[] info){ 
     /*string[0] = Name 
     * string[1] = Path 
     */ 
     destinationsTable.Rows.Add(info[0], "BDR", info[1]); 
     //destinationsTable.Update(); 
     //destinationsTable.Refresh(); 
     checkTableRowCount(); 
    } 

這些方法曾經任職於行添加到DataGridView。所述info數組值是從該方法稱爲AddDialog.cs另一類通過:

private void destAddButton_Click(object sender, EventArgs e) 
    { 
     ac = new AutoCheck(); 
     if(destNameTextbox.TextLength <= 0 || destNameTextbox.Text == null){ 
      MessageBox.Show("Please enter a name","Error",MessageBoxButtons.OK,MessageBoxIcon.Error); 
     }else if(destPathTextbox.TextLength <= 0 || destPathTextbox.Text == null){ 
      MessageBox.Show("Please select a path", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
     }else if (!Directory.Exists(destPathTextbox.Text)){ 
      MessageBox.Show("Please select a valid path", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
     }else if (isNAS()) 
     { 
      if((destUserTextbox.TextLength <= 0 || destUserTextbox.Text == null) || (destPassTextbox.TextLength <= 0 || destPassTextbox.Text == null)){ 
       MessageBox.Show("Please enter a Username and Password", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
      }else{ 
       //If Name and User/Pass are good, add info to temp array and pass by reference to addNASDestination 
       string[] temp = new string[] { destNameTextbox.Text, destPathTextbox.Text, destUserTextbox.Text, AutoCheck.Encrypt(destPassTextbox.Text) }; 
       ac.addNASDestination(temp); 
       this.Dispose(); 
      } 
     }else{ 
      //Assume its a BDR and add info to temp array and pass by reference to addBDRDestination 
      string[] temp = new string[] {destNameTextbox.Text,destPathTextbox.Text}; 
      ac.addBDRDestination(temp); 
      this.Dispose(); 
     } 
    } 

AddDialog是作爲名稱描述,該請求輸入的用戶的對話,其然後抓住輸入,並把它在一個數組,通過參考addBDRDestinationaddNASDestination傳遞陣列,他們應該將新行添加到DataGridView

這工作不適合我,我也想看看,如果甚至被髮送的數據到addBDRDestinationaddNASDestination通過使用Console.WriteLine輸出傳遞的數據和實現這些方法,但新行未被添加。

我試圖通過將此刷新DataGridView(這也是仍然在我的貼碼):

destinationsTable.Update(); 
destinationsTable.Refresh(); 

我也試過這個教程:http://csharp.net-informations.com/datagridview/csharp-datagridview-add-column.htm 它約等於我在做什麼,但現在它增加了整個數組,而不是像我那樣分解它。

我也嘗試創建一個DataRow如下所示:https://social.msdn.microsoft.com/Forums/windows/en-US/f12158b3-4510-47cb-b152-409489c3a51a/how-to-add-rows-in-datagridview-programmatically?forum=winformsdatacontrols

DataRow dr = this.dt.NewRow(); 
dr["a"] = "ax"; 
dr["b"] = "add item"; 
destinationsTable.Rows.Add(dr); 

我試圖啓用和禁用AllowUserToAddRows但沒有任何效果。

我也試過這樣:

DataGridViewRow row = (DataGridViewRow)destinationsTable.Rows[0].Clone(); 
row.Cells[0].Value = info[0]; 
row.Cells[1].Value = "BDR"; 
row.Cells[2].Value = info[1]; 
destinationsTable.Rows.Add(row); 

我也不太確定什麼我可以嘗試,因爲這爲我工作之前,現在它不是。

我認爲值得一提的是AddDialog.csAutoCheck.cs是不同的類/源文件,但在相同的命名空間AutoCheck

AddDialog.cs即時通訊從AutoCheck.cs通過添加AutoCheck ac = new AutoCheck();訪問方法。 AutoCheckAddDialog也是如此。

有沒有其他方法可以添加行?或者我正在做我的當前代碼有問題?非常感謝!

+0

您是否添加了列?在DataGridView.Columns可見。如果有的話,請添加您拋出的異常。 – Bagerfahrer

+0

嗯,我確實已經添加了設計器中的列,如果這就是你正在談論的內容。除非我需要在添加行時再次定義它們。至於異常,我沒有得到任何異常,並不知道如果我手動添加try catch子句添加什麼異常。 – xR34P3Rx

+0

當沒有錯誤發生時,那麼你應該過時的設計你的設計。這是明智的,我們無法查看您的代碼中的具體問題。通過你的方法調試,直到你的行應該被添加。或者重寫你的代碼。 – Bagerfahrer

回答

0

我不知道你正面臨着問題:

public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
      dgv.Columns.Add("cell_one", "Cell 1"); 
      dgv.Columns.Add("cell_two", "Cell 2"); 
      dgv.Columns.Add("cell_three", "Cell 3"); 
      dgv.Columns.Add("cell_four", "Cell 4"); 
      dgv.Columns.Add("cell_five", "Cell 5"); 
      addNASDestination(new string[] { "1", "3", "4", "5" }); 
     } 

     public void addNASDestination(string[] info) 
     { 
      /*string[0] = Name 
      * string[1] = Path 
      * string[2] = Username 
      * string[3] = Password - Needs to be passed to XML encrypted. Not displayed in the table at all 
      */ 
      dgv.Rows.Add(info[0], "NAS", info[1], info[2], info[3]); 
      //checkTableRowCount(); 
     } 
    } 

它編譯和按預期工作。


即使是這樣:

public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 

      DataTable dt = new DataTable("main"); 
      dt.Columns.Add("column_one", typeof(string)); 
      dt.Columns.Add("column_two", typeof(string)); 
      dt.Columns.Add("column_three", typeof(string)); 
      dt.Columns.Add("column_four", typeof(string)); 
      dt.Columns.Add("column_five", typeof(string)); 
      dgv.DataSource = dt; 
      addAnyRow(); 
     } 

     public void addAnyRow() { 
      var dt = (DataTable)dgv.DataSource; 
      var row = dt.NewRow(); 
      row["column_one"] = "1"; 
      row["column_two"] = "2"; 
      row["column_three"] = "3"; 
      row["column_four"] = "4"; 
      row["column_five"] = "5"; 
      dt.Rows.Add(row); 
    } 
} 

在這裏,我分隔它們:

namespace StackOverflow___46658777 
{ 
    public static class Global 
    { 
     public static DataGridView DestinationTable; 
    } 

    public partial class Form1 : Form 
    { 
     public Form1(bool dataTableOrManual = false) 
     { 
      InitializeComponent(); 
      Global.DestinationTable = dgv; 

      if (dataTableOrManual) { 
       var dt = new DataTable("main"); 
       dt.Columns.Add("column_one", typeof(string)); 
       dt.Columns.Add("column_two", typeof(string)); 
       dt.Columns.Add("column_three", typeof(string)); 
       dt.Columns.Add("column_four", typeof(string)); 
       dt.Columns.Add("column_five", typeof(string)); 
       dgv.DataSource = dt; 
       new TAutoCheck().AddAnyRow(); 
       new TAutoCheck().AddAnyRow(); 
       new TAutoCheck().AddAnyRow(); 
      } else { 
       dgv.Columns.Add("cell_one", "Cell 1"); 
       dgv.Columns.Add("cell_two", "Cell 2"); 
       dgv.Columns.Add("cell_three", "Cell 3"); 
       dgv.Columns.Add("cell_four", "Cell 4"); 
       dgv.Columns.Add("cell_five", "Cell 5"); 
       new TAutoCheck().AddNASDestination(new string[] { "1", "3", "4", "5" }); 
      } 
     } 
    } 

    public class TDialog : Form 
    { 
     public TDialog() 
     { 
      //anyButton.Click += validateRequest; 
     } 

     void validateRequest(object sender, EventArgs args) 
     { 
      new TAutoCheck().AddNASDestination(new string[] { "your", "validated", "strings", "are", "here" }); 
     } 
    } 

    public class TAutoCheck 
    { 
     public TAutoCheck() { } 

     public void AddAnyRow() 
     { 
      var dt = (DataTable)Global.DestinationTable.DataSource; 
      var row = dt.NewRow(); 
      row["column_one"] = "1"; 
      row["column_two"] = "2"; 
      row["column_three"] = "3"; 
      row["column_four"] = "4"; 
      row["column_five"] = "5"; 
      dt.Rows.Add(row); 
     } 

     public void AddNASDestination(string[] info) 
     { 
      /*string[0] = Name 
      * string[1] = Path 
      * string[2] = Username 
      * string[3] = Password - Needs to be passed to XML encrypted. Not displayed in the table at all 
      */ 
      Global.DestinationTable.Rows.Add(info[0], "NAS", info[1], info[2], info[3]); 
      //checkTableRowCount(); 
     } 
    } 
} 

按預期工作。我希望它離你的分離距離很近,因爲你沒有多說關於你的類和DataGridView之間的交流方式。分離自己的源文件中的類不會改變裏面的類和函數的行爲。

+0

將它們分開。我擁有它的方式是它們在不同的源文件,不同的類但名稱空間相同。是啊,如果你把它全部放在同一個班級,當然它的作品,但這不是我如何設置。 – xR34P3Rx

+0

你的DataTable是綁定到你的DataGridView.DataSource的嗎?你在談論兩個類,你在哪裏定義/綁定你的DataTable到DataGridView?除非將另一個DataTable綁定到DataGridView.DataSource,否則只能使用這一個和相同的DataTable。 – Bagerfahrer

+0

獨立的源文件中的類或邏輯沒有理由不起作用,除非你通過設計製造混亂,並打破了類之間的交流。看看編輯的答案。嘗試從中適應。 – Bagerfahrer