Adding rows to DataGridView programmatically
-
Hi, Project ------- I have this list box that will get a list of items from a database. Upon choosing an item, it will populate the DataGridView with the data, this data is different for different items, e.g. one might have a size field and another wouldn't or an item might have 5 entries but another 9, etc. This populated data is from a table that has a unique ID (key), the field name, the field value and the foreign key to link to the item table. Problem 1 --------- Currently, I'm trying to add an item into the database, how can I create a DataGridView that would allow me to add rows to it. If I bind it to a table I get the error that table.rows.add() is not allowed on a bound DataGridView. I have to presume that I can just unbind the table and add the rows of data manually. I'm wondering if there is an easier way of doing it. Problem 2 --------- Further to the above, if I want to edit an item, how can I go about adding a new row if the columns are bound. I require (during editing) that columns because more than 1 user might be editing the same item at the same time. How would I go about doing this? Anyone has any article to point me to? Thanks. Desmond
-
Hi, Project ------- I have this list box that will get a list of items from a database. Upon choosing an item, it will populate the DataGridView with the data, this data is different for different items, e.g. one might have a size field and another wouldn't or an item might have 5 entries but another 9, etc. This populated data is from a table that has a unique ID (key), the field name, the field value and the foreign key to link to the item table. Problem 1 --------- Currently, I'm trying to add an item into the database, how can I create a DataGridView that would allow me to add rows to it. If I bind it to a table I get the error that table.rows.add() is not allowed on a bound DataGridView. I have to presume that I can just unbind the table and add the rows of data manually. I'm wondering if there is an easier way of doing it. Problem 2 --------- Further to the above, if I want to edit an item, how can I go about adding a new row if the columns are bound. I require (during editing) that columns because more than 1 user might be editing the same item at the same time. How would I go about doing this? Anyone has any article to point me to? Thanks. Desmond
Answer to problem 1 ------------------- 1. Add a datagridview to your form. 2. Dont add any column to it add a quote to retrieve data from the database 1. connectionstring e.g string conStr="Add the relevent connection to the database" 2. a function to retrieve the data. Create a class called it DataAccess
using system.data; using system.data.oleddb; public class DataAccess { public void Show(string sql, string con, datagridview dt) { //add logic to retieve record from database } }
Then you can call this class to pupulate any datagridview with any number of rows or columns without any modifications. If you want a complete quote let me know (lnkgoeng@webmail.co.za)