简体   繁体   English

填充C#WinForms DataGridview

[英]Populating a C# WinForms DataGridview

I have a DataGridview on my form, as below, which the users can enter rows of data. 如下所示,我在窗体上有一个DataGridview,用户可以输入数据行。 As you can see one of the columns is a Combobox column. 如您所见,列之一是“组合框”列。

保存前的网格

When a user clicks the save button on the form, the entries they have added to the grid are saved into a table in the database. 当用户单击表单上的“保存”按钮时,他们添加到网格中的条目将保存到数据库的表中。

What I need it to do once this is done, is when the form is opened next time, any entries where the Location is "Stock" are still sitting in the grid. 完成此操作后,我需要做的就是下次打开表单时,“位置”为“股票”的所有条目仍位于网格中。 I have tried the below code but just get the below issue. 我试过下面的代码,但只是得到以下问题。

        SqlConnection con = new SqlConnection(ConString);
        con.Open();
        SqlCommand cmd1 = new SqlCommand("SELECT ProductID, Location FROM ProductLoc WHERE Location LIKE 'Stock'", con);
        cmd1.CommandType = CommandType.Text;
        DataTable dt = new DataTable();
        SqlDataReader re = cmd1.ExecuteReader();
        dt.Load(re);
        ProductLocGrid.DataSource = dt;

Result when the form loads: 表单加载时的结果:

表单加载后的网格

As you can see it adds the data from the SELECT statement as two new columns, instead of populating the existing columns with the data. 如您所见,它将SELECT语句中的数据添加为两个新列,而不是用数据填充现有列。 I need it to load the form with the existing data populated, and retaining the ability to add new rows and also change the Location column of the existing data. 我需要它来加载填充有现有数据的表单,并保留添加新行以及更改现有数据的Location列的功能。 Above it's loading the existing Location as a text field, instead of choosing that option from the combobox column. 在其上方,将现有位置作为文本字段加载,而不是从组合框列中选择该选项。

I read somewhere that the columns in the database and the columns in the datagridview need to be the same - they are. 我在某处读到,数据库中的列和datagridview中的列必须相同-它们是相同的。

I'm completely stumped, and this is the last bit of work I need to do on this project and I'm finally done (only taken 6 months of different people getting involved and requesting changes - it's nothing like the original spec!). 我很沮丧,这是我在该项目上需要做的最后工作,我终于完成了(只花了6个月的时间,不同的人参与其中并要求更改-这与原始规范完全不同!)。

Any help would be gratefully received. 任何帮助将不胜感激。

Many thanks in advance. 提前谢谢了。

UPDATE: Following a suggestion from Fabio in the comments, i added the line 更新:根据法比奥在评论中的建议,我添加了这一行

ProductLocGrid.AutoGenerateColumns = false;

When i load the form, the grid now looks like this: 当我加载表单时,网格现在看起来像这样:

添加评论后的网格

As you can see, the columns are no longer duplicating, however the data isn't being populated either. 如您所见,这些列不再重复,但是也不会填充数据。

Many thanks to Fabio's suggestion to add 非常感谢Fabio的建议

DataGridView.AutoGenerateColumns = false;

I added this and at first it didn't work, however when I found I had done a typo and corrected my column name it worked perfectly. 我添加了这个功能,但起初它不起作用,但是当我发现输入错误并更正了我的列名时,它可以很好地工作。

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

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