无法添加行C#DataGridView
我正在处理这个小小的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
也是如此。
有没有其他方法可以添加行?或者我正在做我的当前代码有问题?非常感谢!
我不知道你正面临着问题:
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 *___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之间的交流方式。分离自己的源文件中的类不会改变里面的类和函数的行为。
将它们分开。我拥有它的方式是它们在不同的源文件,不同的类但名称空间相同。是啊,如果你把它全部放在同一个班级,当然它的作品,但这不是我如何设置。 – xR34P3Rx
你的DataTable是绑定到你的DataGridView.DataSource的吗?你在谈论两个类,你在哪里定义/绑定你的DataTable到DataGridView?除非将另一个DataTable绑定到DataGridView.DataSource,否则只能使用这一个和相同的DataTable。 – Bagerfahrer
独立的源文件中的类或逻辑没有理由不起作用,除非你通过设计制造混乱,并打破了类之间的交流。看看编辑的答案。尝试从中适应。 – Bagerfahrer
您是否添加了列?在DataGridView.Columns可见。如果有的话,请添加您抛出的异常。 – Bagerfahrer
嗯,我确实已经添加了设计器中的列,如果这就是你正在谈论的内容。除非我需要在添加行时再次定义它们。至于异常,我没有得到任何异常,并不知道如果我手动添加try catch子句添加什么异常。 – xR34P3Rx
当没有错误发生时,那么你应该过时的设计你的设计。这是明智的,我们无法查看您的代码中的具体问题。通过你的方法调试,直到你的行应该被添加。或者重写你的代码。 – Bagerfahrer