私はこの小さなC#プロジェクトの作業に取り組んでおり、データをDataGridView
コントロールに追加する必要があります。私は前に働いていましたが、悲しいことに、私は自分のコードにいくつかの変更を加えたので、以前はどうなっていたのか覚えていません。私が覚えているのは、VisualStudioではForm.Designer.cs
に戻り、テストの前にいくつかのコントロールをpublic static
に変更していたので、行を追加できたということです。私はそれについてオンラインで何かを読んで、そうしないように提案したので、私は自分のコードを変更したのです。行を追加できませんC#DataGridView
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
に行を追加するために働いていた:とにかく前進し
は、私はメソッドを持って、メインクラスAutoCheck.cs
を持っています。 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
に追加する必要があります。
これは私のために働いていないと私は、データがさえ、データが渡されている出力にConsole.WriteLine
の使用によってaddBDRDestination
またはaddNASDestination
にオーバー送られているかどうかを確認しようとしたと、それはそれらのメソッドを到達ですが、新しいです行が追加されていません。私は今が、やっているものと同じ程度http://csharp.net-informations.com/datagridview/csharp-datagridview-add-column.htm その:
destinationsTable.Update();
destinationsTable.Refresh();
は、私もこのチュートリアルを試してみました:
は、私は(これは私の投稿のコードに残っもある)、これを追加することにより、DataGridView
をリフレッシュしてみましたそれは私のやり方でそれを分解するのではなく、配列全体を追加します。
ここに示したように、私も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