简体   繁体   中英

How can I get DataGridView combobox cell value to be written in the MessageBox in C#?

I want to read the the combobox column as shown in the picture, i've tried using string mode = dt.Rows[0]["Mode"].ToString(); But it doesn't work, it read the USID column instead. How to read the combobox value?

datatable

    private void getmode()

{

//from here 
var modelist = new List<string>() { "Reg0", "Basic", "Extended" };
dgvmode.DataSource = modelist;
dgvmode.HeaderText = "Mode";
dtDataGridView.Columns.Add(dgvmode); 
// until here, i've create a combobox column in my datatable
string mode = dt.Rows[i]["Mode"].ToString(); // this line got error, it can't read the "Mode" column and say it is not exist, but in my picture, it is exist as a combobox column.

Messagebox.Show(mode); // here to show i get the combobox value

}

datatable is unable to reada datagridview feature, thus, when i using

yourdatatable.Rows[i]["Mode"].ToString();

it will comes out an error (something likie "Mode" column doesnt exist)

even you change the "Mode" to an integer that indicate the column you want to read, it will automatically read the column that create by datatable only. For example: yourdatatable.Rows[i][0].ToString(); it wont read the combobox column and it will read normal column instead (will not read combobox column ("Mode"), which is the first column. But it will read the column ("USID"), which is the second column).

then, i need to use datagridview like the code below to read the combobox value: yourDataGridView.Rows[i].Cells[0].Value.ToString()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM