1

I have a datagrid, with a number of rows. I want to get the value of cell[0]. In the window form I was using this code:

 for (int i = 0; i < dataGridView1.Rows.Count; i++)
        {
            if (dataGridView1.Rows[i].Cells[0].Value == null)
            {
                //do somthing
            }
         }

The problem is, I do not know how to get the value of the cell, as this code does not work in WPF.

neminem
  • 2,658
  • 5
  • 27
  • 36
Mahmoud Samy
  • 439
  • 4
  • 20
  • Your code occurs an error when you compile it, try to correct your code before asking question :) – aEk BKF Jun 07 '14 at 09:53
  • Remove the last " ) " and try this : comboBox3.Text = dataGridView1.Rows[i].Cells[0].Value.ToString(); – aEk BKF Jun 07 '14 at 09:54
  • @Mahmoud - I have removed the typos from your question. Make sure to post correct code otherwise you will get answers correcting that typos for you instead of actual answers you expecting from community. – Rohit Vats Jun 07 '14 at 09:55
  • "Which is it: `dataGridView` or `DataGrid`? `Winforms` or `WPF`? – TaW Jun 07 '14 at 09:56
  • @Mahmoud - What's the first cell look like? Can't you get the value from underlying data object binded with dataGrid? – Rohit Vats Jun 07 '14 at 09:58
  • @RohitVats I edited my question and my code it's working in a window forms i want same in a wpf – Mahmoud Samy Jun 07 '14 at 10:05
  • @Mahmoud - You should read more about WPF. WPF doesn't work same like WinForms. WPF is more powerful than WinForms in terms of data abstraction it provides. WPF is more oriented towards playing with underlying data source instead of playing with UI controls. That's why no Row property exist for WPF dataGrid because you can get the values from underlying data object binded to dataGrid. Must read - [Transitioning from WinForms to WPF](http://stackoverflow.com/a/15684569/632337). – Rohit Vats Jun 07 '14 at 10:10
  • Edit your code like this : if (dataGridView1.Rows[i].Cells[0].Value != null) { //do somthing } – aEk BKF Jun 07 '14 at 10:13
  • @RohitVats Ok i know WPF doesn't work same like WinForms ,i want code wpf work same like this code – Mahmoud Samy Jun 07 '14 at 10:21
  • @Mahmoud - I have added an answer for that. – Rohit Vats Jun 07 '14 at 10:26

2 Answers2

0

== is a comparison operator. = is used for assignment:

comboBox3.Text = dataGridView1.Rows[i].Cells[0].Value.ToString();
bobthedeveloper
  • 3,733
  • 2
  • 15
  • 31
Raging Bull
  • 18,593
  • 13
  • 50
  • 55
0

Like I mentioned in the comments, you need to read more on WPF and bindings that how it works because value which you are trying to get from UI can easily be fetched from underlying data object.

Say you have binded dataGrid to some list ObservableCollection<MyObject> and first column of dataGrid is binded to property Name. You can get value for first cell simply like this:

for (int i = 0; i < dataGridView1.Items.Count; i++)
{
    string value = ((MyObject)dataGridView1.Items[0]).Name;
    if (String.IsNullOrEmpty(textBlock.Text))
    {
       // do something.
    }
}

That being said, assuming first cell is simple DataGridTextColumn, you can get the value in traditional WinForms way in WPF like this:

for (int i = 0; i < dataGridView1.Items.Count; i++)
{
    TextBlock textBlock = dataGridView1.Columns[0]
                         .GetCellContent(dataGridView1.Items[i]) as TextBlock;
    if (textBlock != null)
    {
        if (String.IsNullOrEmpty(textBlock.Text))
        {
            // do something.
        }
    }
}
Rohit Vats
  • 79,502
  • 12
  • 161
  • 185