11 Replies - 6710 Views - Last Post: 02 August 2012 - 03:01 PM Rate Topic: -----

#1 Cmore86  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 01-August 12

Rookie needing help pulling data from a DataGridView

Posted 02 August 2012 - 12:31 PM

I have been successful in connecting to the DSN. However I am having trouble extracting the data from the datagridview.

If anyone has any hints or a direction they can point me to, it will be appreciated.

The error I am getting now is

Error 1 An object reference is required for the non-static field, method, or property 'System.Windows.Forms.DataGridView.Rows.get'

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Odbc;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace RSQ_App_V1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            try
            {
                //Initialize variables
                int contactsWaiting = 0;
                //rtcsqssummary = callswaiting
                int oldestContact = 0;
                //rtcsqssummary = oldestcontact
                int totalContacts = 0;
                //rtcsqssummary = totalcalls
                int contactsHandled = 0;
                //rtcsqssummary = callshandled
                int contactsAbandoned = 0;
                //rtcsqssummary = callsabandoned
                double percentageHandled = 0;
                // Establishes Connection to UCCX
                OdbcConnection connection = new OdbcConnection();
                connection.ConnectionString = "DSN=UCCX";
                // Selects Total Calls   
                string queryString = "SELECT totalcalls FROM rtcsqssummary WHERE csqname='RSQ-Spanish' OR csqname='RSQ-English'";
                // Query Command
                OdbcCommand command = new OdbcCommand(queryString, connection);
                ////Error Fails
                //MessageBox.Show("IT Breaks after this....");
                //Open Connection
                connection.Open();
                ////Error Pass
                //MessageBox.Show("You solved the connection issue!");
                //Executes Data Reader
                OdbcDataReader reader = command.ExecuteReader();
                DataTable dt = new DataTable("UCCXData");
                dt.Load(reader);                           
                reader.Close();
                connection.Close();
                //fill the dataview
                dvData.DataSource = dt;
                textBox1.Text = DataGridView.Rows[1].Cells[1].Value;
 }
            catch (Exception ex)
            {
                MessageBox.Show("there was an error - " + ex);
                textBox1.Text = " " + ex;
                
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'dataSet1.rtcsqssummary' table. You can move, or remove it, as needed.
            this.rtcsqssummaryTableAdapter.Fill(this.dataSet1.rtcsqssummary);

        }

        
      
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Rookie needing help pulling data from a DataGridView

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3666
  • View blog
  • Posts: 11,497
  • Joined: 05-May 12

Re: Rookie needing help pulling data from a DataGridView

Posted 02 August 2012 - 01:23 PM

You should post the complete error including line number. Or at least tell us which line in your code snippet is having the problem.

Anyway the error is pretty self explanatory. You need an object reference. dvData is an object reference.

The line should be:
textBox1.Text = dvData.Rows[1].Cells[1].Value;


Was This Post Helpful? 1
  • +
  • -

#3 Cmore86  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 01-August 12

Re: Rookie needing help pulling data from a DataGridView

Posted 02 August 2012 - 01:47 PM

View PostSkydiver, on 02 August 2012 - 01:23 PM, said:

You should post the complete error including line number. Or at least tell us which line in your code snippet is having the problem.

Anyway the error is pretty self explanatory. You need an object reference. dvData is an object reference.

The line should be:
textBox1.Text = dvData.Rows[1].Cells[1].Value;



Error 1 Cannot implicitly convert type 'object' to 'string'. An explicit conversion exists (are you missing a cast?) \\cmi-fs-1\redirected folders - Offsite\cesarm\My Documents\Resident Services App\RSQ App\Project\RSQ App\RSQ App Projects\RSQ App Simple\RSQ(Simple).cs 48 33 RSQ App V1


Is the error I am getting now.

Seems that I need some kind of conversion?
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3666
  • View blog
  • Posts: 11,497
  • Joined: 05-May 12

Re: Rookie needing help pulling data from a DataGridView

Posted 02 August 2012 - 02:00 PM

Yes, typically, the Value field of each cell is an object type. As you know, C# wants to be type safe, so it'll be up to you to convert that value to a string so that the textbox's Text field can be set.

Most people abuse ToString(), but that is a rant for another time.
Was This Post Helpful? 1
  • +
  • -

#5 Cmore86  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 01-August 12

Re: Rookie needing help pulling data from a DataGridView

Posted 02 August 2012 - 02:03 PM

View PostSkydiver, on 02 August 2012 - 02:00 PM, said:

Yes, typically, the Value field of each cell is an object type. As you know, C# wants to be type safe, so it'll be up to you to convert that value to a string so that the textbox's Text field can be set.

Most people abuse ToString(), but that is a rant for another time.



Thanks when doing so it shows this error: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
at System.Collections.ArrayList.get_Item(Int32 index)
at System.Windows.Forms.DataGridViewCellCollection.get_Item(Int32 index)
at RSQ_App_V1.Form1..ctor() in \\cmi-fs-1\redirected folders - Offsite\cesarm\My Documents\Resident Services App\RSQ App\Project\RSQ App\RSQ App Projects\RSQ App Simple\RSQ(Simple).cs:line 48

Is there a way to avoid converting to string?
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3666
  • View blog
  • Posts: 11,497
  • Joined: 05-May 12

Re: Rookie needing help pulling data from a DataGridView

Posted 02 August 2012 - 02:34 PM

Do you have at least 2 rows and 2 columns in the DataTable? Remember that indices are 0 based.

In the case of the code you posted above, there is no getting away from calling ToString(). It is the appropriate call in that case.

Expand spoiler to see my rant.
Spoiler

Was This Post Helpful? 3
  • +
  • -

#7 superken81  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 24
  • Joined: 01-August 12

Re: Rookie needing help pulling data from a DataGridView

Posted 02 August 2012 - 02:36 PM

View PostCmore86, on 02 August 2012 - 02:03 PM, said:

View PostSkydiver, on 02 August 2012 - 02:00 PM, said:

Yes, typically, the Value field of each cell is an object type. As you know, C# wants to be type safe, so it'll be up to you to convert that value to a string so that the textbox's Text field can be set.

Most people abuse ToString(), but that is a rant for another time.



Thanks when doing so it shows this error: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
at System.Collections.ArrayList.get_Item(Int32 index)
at System.Windows.Forms.DataGridViewCellCollection.get_Item(Int32 index)
at RSQ_App_V1.Form1..ctor() in \\cmi-fs-1\redirected folders - Offsite\cesarm\My Documents\Resident Services App\RSQ App\Project\RSQ App\RSQ App Projects\RSQ App Simple\RSQ(Simple).cs:line 48

Is there a way to avoid converting to string?


I believe you want "textBox1.Text = DataGridView.Rows[0].Cells[0].Value.ToString();", but it may be Cells[1].. either way the issue looks like it is probably in your Rows[] or Cells[] index value.
Was This Post Helpful? 1
  • +
  • -

#8 Cmore86  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 01-August 12

Re: Rookie needing help pulling data from a DataGridView

Posted 02 August 2012 - 02:42 PM

View PostSkydiver, on 02 August 2012 - 02:34 PM, said:

Do you have at least 2 rows and 2 columns in the DataTable? Remember that indices are 0 based.

In the case of the code you posted above, there is no getting away from calling ToString(). It is the appropriate call in that case.

Expand spoiler to see my rant.
Spoiler



Haha the last one is hilarious. So far I am only pulling one row and then adding more. I am eventually going to do away with the datagridview and use labels to call the values. Trying to get the architecture and reverse engineer an existing web page and moving it to a desktop mini windows form.

Even when doing
label1.Text = Convert.ToString(dvData.Rows[1].Cells[1].Value);


I get this error:
System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
at System.Collections.ArrayList.get_Item(Int32 index)
at System.Windows.Forms.DataGridViewCellCollection.get_Item(Int32 index)
at RSQ_App_V1.Form1..ctor() in \\cmi-fs-1\redirected folders - Offsite\cesarm\My Documents\Resident Services App\RSQ App\Project\RSQ App\RSQ App Projects\RSQ App Simple\RSQ(Simple).cs:line 50

The connecting part had me banging my head against the wall, this is just confusing to get it to work.

The original application code is in VB.Net

Thanks for all your help. Helping me out a lot.

View Postsuperken81, on 02 August 2012 - 02:36 PM, said:

View PostCmore86, on 02 August 2012 - 02:03 PM, said:

View PostSkydiver, on 02 August 2012 - 02:00 PM, said:

Yes, typically, the Value field of each cell is an object type. As you know, C# wants to be type safe, so it'll be up to you to convert that value to a string so that the textbox's Text field can be set.

Most people abuse ToString(), but that is a rant for another time.



Thanks when doing so it shows this error: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
at System.Collections.ArrayList.get_Item(Int32 index)
at System.Windows.Forms.DataGridViewCellCollection.get_Item(Int32 index)
at RSQ_App_V1.Form1..ctor() in \\cmi-fs-1\redirected folders - Offsite\cesarm\My Documents\Resident Services App\RSQ App\Project\RSQ App\RSQ App Projects\RSQ App Simple\RSQ(Simple).cs:line 48

Is there a way to avoid converting to string?


I believe you want "textBox1.Text = DataGridView.Rows[0].Cells[0].Value.ToString();", but it may be Cells[1].. either way the issue looks like it is probably in your Rows[] or Cells[] index value.



Attempted with the code you provided and got this error.
label1.Text = DataGridView.Rows[0].Cells[0].Value.ToString(); 

Error 1 An object reference is required for the non-static field, method, or property 'System.Windows.Forms.DataGridView.Rows.get' \\cmi-fs-1\redirected folders - Offsite\cesarm\My Documents\Resident Services App\RSQ App\Project\RSQ App\RSQ App Projects\RSQ App Simple\RSQ(Simple).cs 50 30 RSQ App V1
Was This Post Helpful? 0
  • +
  • -

#9 superken81  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 24
  • Joined: 01-August 12

Re: Rookie needing help pulling data from a DataGridView

Posted 02 August 2012 - 02:43 PM

View PostCmore86, on 02 August 2012 - 02:39 PM, said:

View PostSkydiver, on 02 August 2012 - 02:34 PM, said:

Do you have at least 2 rows and 2 columns in the DataTable? Remember that indices are 0 based.

In the case of the code you posted above, there is no getting away from calling ToString(). It is the appropriate call in that case.

Expand spoiler to see my rant.
Spoiler



Haha the last one is hilarious. So far I am only pulling one row and then adding more. I am eventually going to do away with the datagridview and use labels to call the values. Trying to get the architecture and reverse engineer an existing web page and moving it to a desktop mini windows form.

Even when doing
label1.Text = Convert.ToString(dvData.Rows[1].Cells[1].Value);


I get this error:
System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
at System.Collections.ArrayList.get_Item(Int32 index)
at System.Windows.Forms.DataGridViewCellCollection.get_Item(Int32 index)
at RSQ_App_V1.Form1..ctor() in \\cmi-fs-1\redirected folders - Offsite\cesarm\My Documents\Resident Services App\RSQ App\Project\RSQ App\RSQ App Projects\RSQ App Simple\RSQ(Simple).cs:line 50

The connecting part had me banging my head against the wall, this is just confusing to get it to work.

The original application code is in VB.Net

Thanks for all your help. Helping me out a lot.


Looks like "label1.Text = Convert.ToString(dvData.Rows[1].Cells[1].Value);" may have the same issue with index values, but you could write it like this "label1.Text = Convert.ToString(dvData.Rows[0].Cells[0].Value).ToString();".

This "label1.Text = Convert.ToString(dvData.Rows[0].Cells[0].Value).ToString();" will give you the first row, and the data from the first column.

And the way you have it "label1.Text = Convert.ToString(dvData.Rows[1].Cells[1].Value).ToString();" will give you the second row, and the data from the second column.
Was This Post Helpful? 1
  • +
  • -

#10 Cmore86  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 01-August 12

Re: Rookie needing help pulling data from a DataGridView

Posted 02 August 2012 - 02:45 PM

View Postsuperken81, on 02 August 2012 - 02:43 PM, said:

View PostCmore86, on 02 August 2012 - 02:39 PM, said:

View PostSkydiver, on 02 August 2012 - 02:34 PM, said:

Do you have at least 2 rows and 2 columns in the DataTable? Remember that indices are 0 based.

In the case of the code you posted above, there is no getting away from calling ToString(). It is the appropriate call in that case.

Expand spoiler to see my rant.
Spoiler



Haha the last one is hilarious. So far I am only pulling one row and then adding more. I am eventually going to do away with the datagridview and use labels to call the values. Trying to get the architecture and reverse engineer an existing web page and moving it to a desktop mini windows form.

Even when doing
label1.Text = Convert.ToString(dvData.Rows[1].Cells[1].Value);


I get this error:
System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
at System.Collections.ArrayList.get_Item(Int32 index)
at System.Windows.Forms.DataGridViewCellCollection.get_Item(Int32 index)
at RSQ_App_V1.Form1..ctor() in \\cmi-fs-1\redirected folders - Offsite\cesarm\My Documents\Resident Services App\RSQ App\Project\RSQ App\RSQ App Projects\RSQ App Simple\RSQ(Simple).cs:line 50

The connecting part had me banging my head against the wall, this is just confusing to get it to work.

The original application code is in VB.Net

Thanks for all your help. Helping me out a lot.


Looks like "label1.Text = Convert.ToString(dvData.Rows[1].Cells[1].Value);" may have the same issue with index values, but you could write it like this "label1.Text = Convert.ToString(dvData.Rows[0].Cells[0].Value).ToString();".

This "label1.Text = Convert.ToString(dvData.Rows[0].Cells[0].Value).ToString();" will give you the first row, and the data from the first column.

And the way you have it "label1.Text = Convert.ToString(dvData.Rows[1].Cells[1].Value).ToString();" will give you the second row, and the data from the second column.



Thank you so much. I see it now. Now I just have to build up from here.

Thank you for your help!
Was This Post Helpful? 0
  • +
  • -

#11 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3666
  • View blog
  • Posts: 11,497
  • Joined: 05-May 12

Re: Rookie needing help pulling data from a DataGridView

Posted 02 August 2012 - 02:56 PM

View Postsuperken81, on 02 August 2012 - 02:43 PM, said:

This "label1.Text = Convert.ToString(dvData.Rows[0].Cells[0].Value).ToString();" will give you the first row, and the data from the first column.


LOL! I'm quite sure that superken81 just did more ToString() abuse just to push my buttons. :lol:
Was This Post Helpful? 1
  • +
  • -

#12 superken81  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 24
  • Joined: 01-August 12

Re: Rookie needing help pulling data from a DataGridView

Posted 02 August 2012 - 03:01 PM

View PostSkydiver, on 02 August 2012 - 02:56 PM, said:

View Postsuperken81, on 02 August 2012 - 02:43 PM, said:

This "label1.Text = Convert.ToString(dvData.Rows[0].Cells[0].Value).ToString();" will give you the first row, and the data from the first column.


LOL! I'm quite sure that superken81 just did more ToString() abuse just to push my buttons. :lol:


HAHA, no that was a copy paste fail, but a very funny one.

View PostCmore86, on 02 August 2012 - 02:45 PM, said:

View Postsuperken81, on 02 August 2012 - 02:43 PM, said:

View PostCmore86, on 02 August 2012 - 02:39 PM, said:

View PostSkydiver, on 02 August 2012 - 02:34 PM, said:

Do you have at least 2 rows and 2 columns in the DataTable? Remember that indices are 0 based.

In the case of the code you posted above, there is no getting away from calling ToString(). It is the appropriate call in that case.

Expand spoiler to see my rant.
Spoiler



Haha the last one is hilarious. So far I am only pulling one row and then adding more. I am eventually going to do away with the datagridview and use labels to call the values. Trying to get the architecture and reverse engineer an existing web page and moving it to a desktop mini windows form.

Even when doing
label1.Text = Convert.ToString(dvData.Rows[1].Cells[1].Value);


I get this error:
System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
at System.Collections.ArrayList.get_Item(Int32 index)
at System.Windows.Forms.DataGridViewCellCollection.get_Item(Int32 index)
at RSQ_App_V1.Form1..ctor() in \\cmi-fs-1\redirected folders - Offsite\cesarm\My Documents\Resident Services App\RSQ App\Project\RSQ App\RSQ App Projects\RSQ App Simple\RSQ(Simple).cs:line 50

The connecting part had me banging my head against the wall, this is just confusing to get it to work.

The original application code is in VB.Net

Thanks for all your help. Helping me out a lot.


Looks like "label1.Text = Convert.ToString(dvData.Rows[1].Cells[1].Value);" may have the same issue with index values, but you could write it like this "label1.Text = Convert.ToString(dvData.Rows[0].Cells[0].Value).ToString();".

This "label1.Text = Convert.ToString(dvData.Rows[0].Cells[0].Value).ToString();" will give you the first row, and the data from the first column.

And the way you have it "label1.Text = Convert.ToString(dvData.Rows[1].Cells[1].Value).ToString();" will give you the second row, and the data from the second column.



Thank you so much. I see it now. Now I just have to build up from here.

Thank you for your help!

Correction of the .ToString() abuse failure..

Looks like "label1.Text = Convert.ToString(dvData.Rows[1].Cells[1].Value);" may have the same issue with index values, but you could write it like this "label1.Text = Convert.ToString(dvData.Rows[0].Cells[0].Value);".

This "label1.Text = Convert.ToString(dvData.Rows[0].Cells[0].Value);" will give you the first row, and the data from the first column.

And the way you have it "label1.Text = Convert.ToString(dvData.Rows[1].Cells[1].Value);" will give you the second row, and the data from the second column.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1