Getting data out of a row in a grid.

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 2204 Views - Last Post: 17 January 2014 - 09:34 AM Rate Topic: -----

#1 jcborland   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 137
  • Joined: 16-December 13

Getting data out of a row in a grid.

Posted 13 January 2014 - 08:27 AM

Hi,
I'm trying to extract data from a grid, it's a Dev Express grid. I have got an object which if I hover over it in debug mode shows me the data I'm after. In the code below if I hover over "row" I can drill down through "Row" and then "ItemArray" and I can see the data I want in an array but I can't find how to get at it.

private void gridShowUsers_Click(object sender, EventArgs e)
        {
            Object row = gridView1.GetFocusedRow();
            
            string [] rowarray = new string [50];

            string txtrow = row.ToString();
            


            
            
            
            //ow.Row.Item.Array

            //object row = gridView1.GetDataRow(10);


        }



Is This A Good Question/Topic? 0
  • +

Replies To: Getting data out of a row in a grid.

#2 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7109
  • View blog
  • Posts: 24,146
  • Joined: 05-May 12

Re: Getting data out of a row in a grid.

Posted 13 January 2014 - 08:42 AM

So why can't you use row.Row.ItemArray?
Was This Post Helpful? 0
  • +
  • -

#3 jcborland   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 137
  • Joined: 16-December 13

Re: Getting data out of a row in a grid.

Posted 14 January 2014 - 01:25 AM

View PostSkydiver, on 13 January 2014 - 08:42 AM, said:

So why can't you use row.Row.ItemArray?

Hi,
I'm not being offered row.Row.ItemArray the only choices I have are row.Equals, .GetHashCode, GetType & .ToString.

Thanks,
Jim.
Was This Post Helpful? 0
  • +
  • -

#4 jcborland   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 137
  • Joined: 16-December 13

Re: Getting data out of a row in a grid.

Posted 14 January 2014 - 02:43 AM

Hi,
I've got some more details of this. I found a solution at :

http://stackoverflow...xpress-xtragrid

But it means having to reference each cell individually. I'm enclosing a screen shot of my code in debug mode and I'm hovering over "thisrow" and you can see the object array "ItemAray" with the 15 cells values, but I don't know how to use the contents of the array. If I type thisrow. I'm expecting to be given ItemArray as an option but I'm not.

I'm enclosing the code below "focusedrow" and "thisrow" are the same thing effectively, "stringrow" does contain the data from the focused row for the column headed "cmpid" so it will work.

Can anyone help?

private void gridShowUsers_Click(object sender, EventArgs e)
        {
            //string[] focusedrow = new string[15];
           
            Object row =  gridView1.GetFocusedRow();
            Object focusedrow = gridView1.GetFocusedDataRow();
            int i = gridView1.GetFocusedDataSourceRowIndex();
            Object thisrow = gridView1.GetDataRow(i);
            string stringrow = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "cmpid").ToString();
            
            
            
        }

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#5 Charles:)   User is offline

  • D.I.C Regular

Reputation: 149
  • View blog
  • Posts: 359
  • Joined: 26-November 09

Re: Getting data out of a row in a grid.

Posted 14 January 2014 - 02:49 AM

Instead of writing:

Object thisrow = gridView1.GetDataRow(1)


Change to:

System.Data.DataRow thisrow = gridView1.GetDataRow(1)


Then you should have access to the ItemArray.

If you declare things as Object then you'll only get the methods that belong to Object, hence you only having Equals, GetHashCode and ToString.

This post has been edited by Charles:): 14 January 2014 - 02:50 AM

Was This Post Helpful? 2
  • +
  • -

#6 jcborland   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 137
  • Joined: 16-December 13

Re: Getting data out of a row in a grid.

Posted 14 January 2014 - 03:54 AM

Hi,
I've got this sorted out now I'm not sure it's a particularly good idea to use it this way. Here what I've done anyway.

private void gridShowUsers_Click(object sender, EventArgs e)
        {
            string[] columns = new string[15];
           
            columns = GetRowColumns();
            
        }
        private string[] GetRowColumns()
        {
            string[] rowarray = new string[15];
            int x = gridView1.GetFocusedDataSourceRowIndex();

            for(int i=0;i<15;i++)
            {
                rowarray[i] = gridView1.GetDataRow(x).ItemArray[i].ToString();
            }
            return rowarray;

           
            
        }



Thanks for your help.
Regards,
Jim.
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7109
  • View blog
  • Posts: 24,146
  • Joined: 05-May 12

Re: Getting data out of a row in a grid.

Posted 14 January 2014 - 06:43 AM

Your allocation on line 3 is pointless since it gets discarded by your assignment on line 5.

You should avoid the multiple calls to GetDataRow() on line 15 since x never changes within that scope. (e.g. See post #5) It'll be easier read and has the side benefit of being more efficient.
Was This Post Helpful? 1
  • +
  • -

#8 jcborland   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 137
  • Joined: 16-December 13

Re: Getting data out of a row in a grid.

Posted 14 January 2014 - 08:25 AM

Hi,
Thanks.

Line 3 & 5 I see that.

I see your point about calling GetDataRow multiple times but I can't see how to get at ItemArray without referring to GetDataRow everything I try fails. In fact if I could reference ItemArray[i] directly I wouldn't feel the need to move it to rowarray[i].

Where is post #5?

Regards,
Jim.
Was This Post Helpful? 0
  • +
  • -

#9 jcborland   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 137
  • Joined: 16-December 13

Re: Getting data out of a row in a grid.

Posted 14 January 2014 - 08:35 AM

Hi Charles,
I didn't see your reply, that's exactly what I was looking for thanks. It's all working now.

Thanks everyone for your input.
Regards,
Jim.
Was This Post Helpful? 0
  • +
  • -

#10 jcborland   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 137
  • Joined: 16-December 13

Re: Getting data out of a row in a grid.

Posted 14 January 2014 - 09:09 AM

Hi,
In case anyone else finds any of this useful here is the final bit of code.

private void gridShowUsers_Click(object sender, EventArgs e)

        {

            //
                // Create an array to hold the data from the cells of one row
                //
                
                string[] columns;

                //
                // Call the method to put the cell values as strings into an array called columns
                //

                columns = GetRowColumns();

        }
private string[] GetRowColumns()
        {


            string[] rowarray = new string[16];
            int x = gridView1.GetFocusedDataSourceRowIndex();
            int j = gridView1.Columns.Count - 1;
            
            {

                //
                // Put the current rows data into focusedrow
                //

                System.Data.DataRow focusedrow = gridView1.GetDataRow(x);

                for (int i = 0; i < j; i++)
                {
                    //
                    // Move the data from focusedrow array to rowarray and convert to string
                    //
                    rowarray[i] = focusedrow[i].ToString();

                }
                return rowarray;



            }

        }
 

Was This Post Helpful? 0
  • +
  • -

#11 Charles:)   User is offline

  • D.I.C Regular

Reputation: 149
  • View blog
  • Posts: 359
  • Joined: 26-November 09

Re: Getting data out of a row in a grid.

Posted 14 January 2014 - 09:20 AM

I'd also advise doing something like this:

Instead of

string[] rowarray = new string[16];


Try

string[] rowarray = new string[gridView1.Columns.Count];


The problem with hard-coding that 16 value is that if your data changes to have more or less columns, your code will break. It helps to think ahead when writing code, try to think which things might change in the future and deal with them accordingly.
Was This Post Helpful? 0
  • +
  • -

#12 jcborland   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 137
  • Joined: 16-December 13

Re: Getting data out of a row in a grid.

Posted 15 January 2014 - 08:45 AM

Thanks Charles, I had done it like this:

int x = gridView1.GetFocusedDataSourceRowIndex();
int j = gridView1.Columns.Count - 1;
string[] rowarray = new string[j+1];



Thanks for all the help. The forum is great. Now on to the next challenge!!
Regards,
Jim.
Was This Post Helpful? 0
  • +
  • -

#13 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7109
  • View blog
  • Posts: 24,146
  • Joined: 05-May 12

Re: Getting data out of a row in a grid.

Posted 15 January 2014 - 08:50 AM

Why the games with "-1" and "+1" ?
Was This Post Helpful? 0
  • +
  • -

#14 jcborland   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 137
  • Joined: 16-December 13

Re: Getting data out of a row in a grid.

Posted 15 January 2014 - 11:04 AM

I use j in a for loop which starts from zero.
Was This Post Helpful? 0
  • +
  • -

#15 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7109
  • View blog
  • Posts: 24,146
  • Joined: 05-May 12

Re: Getting data out of a row in a grid.

Posted 15 January 2014 - 11:12 AM

But you use < j as your comparison for the index. By subtracting one, you will miss the last row.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2