Thomas
Thomas

Reputation: 34188

Select value and text both from DataGridViewComboBoxColumn c#

suppose i have one datagridview and datagridview has one DataGridViewComboBoxColumn. i have populate datagridview combobox column with country name and code.

so now i want that when i am reading datagridview cell value in for loop then i want get DataGridViewComboBoxColumn selected value and text. i could not get the value but not being able to get display text from DataGridViewComboBoxColumn in for loop.

if it is possible then plzz help me with small code.

private void button1_Click(object sender, EventArgs e)
    {
        for (int i = 0; i <= dgList.Rows.Count - 1; i++)
        {
            DataGridViewComboBoxCell cell = dgList.Rows[i].Cells[0] as DataGridViewComboBoxCell;
            int index = cell == null || cell.Value == null ? -1 : cell.Items.IndexOf(cell.Value);
            string strVal = cell.Value.ToString();
        }
    }

thanks

Upvotes: 0

Views: 5236

Answers (3)

NicoRiff
NicoRiff

Reputation: 4883

Cant you just cast the value to the expected type?

Try this:

dgList["columnName", RowIndex].Value

For example

Convert.toInt32(dgList["columnName", RowIndex].Value)

Upvotes: 0

V4Vendetta
V4Vendetta

Reputation: 38200

Can you try accessing the values like FormattedValue and Value, this should give you the two values.

I had tried using a Dictionary<string,string> as DataSource and using the above properties i was able to get both the values.

Upvotes: 1

Vinod
Vinod

Reputation: 4872

Try This:

for (int i = 0; i <= dgList.Rows.Count - 1; i++)
        { 
       string strval=((ComboBox)(dgList.Rows[i].cells[0].FindControl("ComboBox1"))).SelectedValue;
        }

Upvotes: 0

Related Questions