繁体   English   中英

无法添加行C#DataGridView

[英]Unable to add rows C# DataGridView

我正在为这个小C#项目工作,需要将数据添加到DataGridView控件中。 我以前曾经使用过它,但是可悲的是,由于我对代码进行了一些更改,所以我不记得以前是如何使用它的。 我只记得在Form.Designer.cs我回到Form.Designer.cs并在测试之前将一些控件更改为public static ,以便可以添加行。 我在网上阅读了有关此内容的内容,他们建议不要这样做,所以这就是我更改代码的原因。

无论如何,我有主类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名称所描述的那样, AddDialog是一个对话框,它要求用户输入内容,然后抓取输入并将其放入数组中,通过引用将该数组传递给addBDRDestinationaddNASDestination ,他们应该将新行添加到DataGridView

这对我不起作用,我试图通过使用Console.WriteLine来查看是否将数据发送到addBDRDestinationaddNASDestination ,以输出正在传递的数据,并且正在到达这些方法,但新行并未被发送添加。

我尝试通过添加以下内容来刷新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的-编程?论坛= 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 ac = new AutoCheck();AutoCheck.cs访问方法AutoCheck ac = new AutoCheck(); AutoCheck to 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 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之间的通信方式有多少了解。 在自己的源文件中分隔类不会更改内部类和函数的行为。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM