4 Replies - 7525 Views - Last Post: 23 November 2010 - 01:11 PM Rate Topic: -----

#1 mjw85  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 01-October 07

Datagridview to return values in column (to string?)

Posted 18 November 2010 - 02:10 PM

I am working with a Windows form that includes a datagridview - where the user enters their information. Everything works fine with inserting into the database, however, I need to change the datagridview before it enters the database. The user enters a value (ProductNumber) in a specific column of the database, however, I need to convert the ProductNumber into the ID (in a separate column) before it enters the database. (I understand the SELECT part, I tested it with a textbox and it worked fine) - but am confused how to get the ProductNumber, since it is in a datagridview and cannot easily be transferred into a string. (My datagridview also allows multiple entries) - The ID is the primary key from a database table, which needs to be the number entered into my database table. I apologize if I am not being too clear, it is kind of confusing. Is there an easier way to do this? Thanks for any help/suggestions.

strProdNum = dataGridView.Columns[0].ToString();



Is This A Good Question/Topic? 0
  • +

Replies To: Datagridview to return values in column (to string?)

#2 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Datagridview to return values in column (to string?)

Posted 18 November 2010 - 02:37 PM

Something like this:

//Assuming Cell 0 is the productNumber. Also assuming you want to get
//the productNumber on the click event. You can use a number instead of 'e'.
productNumber = dataGridView1.Rows[e.RowIndex].Cells[0].Value);

Was This Post Helpful? 1
  • +
  • -

#3 mjw85  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 01-October 07

Re: Datagridview to return values in column (to string?)

Posted 18 November 2010 - 02:51 PM

View PostSergio Tapia, on 18 November 2010 - 01:37 PM, said:

Something like this:

//Assuming Cell 0 is the productNumber. Also assuming you want to get
//the productNumber on the click event. You can use a number instead of 'e'.
productNumber = dataGridView1.Rows[e.RowIndex].Cells[0].Value);

Thanks so much for your reply - I am playing around with it, but even when I change to a number instead of 'e' I keep getting the error message "'int' does not contain a definition for 'RowIndex' and no extension method 'RowIndex' accepting a first argument of type 'int' could be found (are you missing a using directive or an assembly reference?)

Probably something really simple - but my brain is at it's limit :)
Was This Post Helpful? 0
  • +
  • -

#4 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Datagridview to return values in column (to string?)

Posted 18 November 2010 - 02:52 PM

Please post your code and say what line the error is firing.
Was This Post Helpful? 0
  • +
  • -

#5 mjw85  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 01-October 07

Re: Datagridview to return values in column (to string?)

Posted 23 November 2010 - 01:11 PM

View Postmjw85, on 18 November 2010 - 01:10 PM, said:

I am working with a Windows form that includes a datagridview - where the user enters their information. Everything works fine with inserting into the database, however, I need to change the datagridview before it enters the database. The user enters a value (ProductNumber) in a specific column of the database, however, I need to convert the ProductNumber into the ID (in a separate column) before it enters the database. (I understand the SELECT part, I tested it with a textbox and it worked fine) - but am confused how to get the ProductNumber, since it is in a datagridview and cannot easily be transferred into a string. (My datagridview also allows multiple entries) - The ID is the primary key from a database table, which needs to be the number entered into my database table. I apologize if I am not being too clear, it is kind of confusing. Is there an easier way to do this? Thanks for any help/suggestions.

strProdNum = dataGridView.Columns[0].ToString();




I figured this issue out - thought I would post the solution (worked for me - hopefully will be helpful to someone else! :)

 strProdNum = dataGridView.Rows[e.RowIndex].Cells["PRODUCTNUM"].EditedFormattedValue.ToString();


(["PRODUCTNUM"] was the name of my column)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1