我正在處理這個小小的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
是作爲名稱描述,該請求輸入的用戶的對話,其然後抓住輸入,並把它在一個數組,通過參考addBDRDestination
或addNASDestination
傳遞陣列,他們應該將新行添加到DataGridView
。
這工作不適合我,我也想看看,如果甚至被髮送的數據到addBDRDestination
或addNASDestination
通過使用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.cs
和AutoCheck.cs
是不同的類/源文件,但在相同的命名空間AutoCheck
。
在AddDialog.cs
即時通訊從AutoCheck.cs
通過添加AutoCheck ac = new AutoCheck();
訪問方法。 AutoCheck
至AddDialog
也是如此。
有沒有其他方法可以添加行?或者我正在做我的當前代碼有問題?非常感謝!
您是否添加了列?在DataGridView.Columns可見。如果有的話,請添加您拋出的異常。 – Bagerfahrer
嗯,我確實已經添加了設計器中的列,如果這就是你正在談論的內容。除非我需要在添加行時再次定義它們。至於異常,我沒有得到任何異常,並不知道如果我手動添加try catch子句添加什麼異常。 – xR34P3Rx
當沒有錯誤發生時,那麼你應該過時的設計你的設計。這是明智的,我們無法查看您的代碼中的具體問題。通過你的方法調試,直到你的行應該被添加。或者重寫你的代碼。 – Bagerfahrer