Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Adding rows to DataGridView programmatically

Adding rows to DataGridView programmatically

Scheduled Pinned Locked Moved C#
helpquestiondatabase
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Desmond Lim
    wrote on last edited by
    #1

    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

    L 1 Reply Last reply
    0
    • D Desmond Lim

      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

      L Offline
      L Offline
      lnkgoeng
      wrote on last edited by
      #2

      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)

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups