Saturday 11 February 2012

WPF Get cell Value from dataGrid. Cell(i,j))

I had searched a lot, but all answers were vague. I have tried on my own and did it.


for (int j = 0; j < dataGrid1.Columns.Count; j++)
            {
                for (int i = 0; i < dataGrid1.Items.Count - 1; i++)
                {
                    string s=(dataGrid1.Items[i] as DataRowView).Row.ItemArray[j].ToString();
                }
            }

i,j are the co-ordinates. So you can play around it.

8 comments:

  1. I need to put this code inside: SuchAnEventDataBindingCompleted(.. )
    I'll find it soon. :)
    Thanks.

    ReplyDelete
  2. A working DataBindingCompletedEvent:


    ...
    dataGrid1.DataContext = dataSet.Tables[0].DefaultView ;
    dataGrid1.AutoGenerateColumns = true;
    ...

    private void dataGrid1_AutoGeneratedColumns(object sender, EventArgs e)
    {
    string s = (dataGrid1.Items[3] as DataRowView).Row.ItemArray[0].ToString();
    MessageBox.Show(s);
    }

    ReplyDelete
  3. That is not a cell! try to change the background color...

    ReplyDelete
  4. I was searching for this for a long time.
    Thank you very much.

    ReplyDelete
  5. Does not work!

    Error : Object reference not set .... on line
    string s=(dataGrid1.Items[i] as DataRowView).Row.ItemArray[j].ToString();

    ReplyDelete
  6. Thank you for this quiet simple solution. There are others, but they are so 'from behind through the chest in the eye' ;-)
    Thank you !

    ReplyDelete
  7. Simple and brilliant, thank you.
    Every other solution was way more complicated than it needed to be.

    ReplyDelete