ahmed salah

ahmed salah

  • NA
  • 530
  • 141.7k

How to get selected text for combox box in datagridview

Mar 10 2017 11:03 PM
I work in windows form c# vs 2015 
 
i need to get text of selected value of combobox inside datagridview
 
i can added success in datagridview but cannot get text for it .
 
I replaced by column already exist his name Country
 
and become Country2 
 
this code of combobox
 
Actually i need to get selected text of country to update data gridview
but i cannot do that so that can you help me in that ?
my update based on updated country where userid=userid
so that i need country text of combobox added 
 
  1. private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)  
  2.        {  
  3.            if (e.ColumnIndex >= 0)  
  4.            {  
  5.                if (dataGridView1.Columns[e.ColumnIndex].Name == "Save")  
  6.                {  
  7.                    indexRow = e.RowIndex; // get the selected Row Index  
  8.                    DataGridViewRow row3 = dataGridView1.Rows[indexRow];  
  9.                    //ComboBox cmbBox = e.Control as ComboBox;  
  10.                    //cmbBox.SelectedIndexChanged += new EventHandler(comboBox_SelectedIndexChanged);  
  11.                    //MessageBox.Show(str);  
  12.                    //dataGridView1.CurrentRow.Cells["Country"].Value.ToString();  
  13.                    // dataGridView1.CurrentRow.Cells[0].Value.ToString();  
  14.                    //QrClasses qrc = new MatrixBarcode.QrClasses();  
  15.                    //string valueupdate = qrc.updatedatagridview(dataGridView1.CurrentRow.Cells[5].Value.ToString(), row3.Cells[2].Value.ToString());  
  16.                    //if (valueupdate != null)  
  17.                    //{  
  18.                    //    MessageBox.Show("??? ?? ??????? ?????");  
  19.                    //}  
  20.                }  
  21.                if (dataGridView1.Columns[e.ColumnIndex].Name == "Edit")  
  22.                {  
  23.   
  24.                    dataGridView1.Columns["Country"].Visible = false;  
  25.                   DataGridViewComboBoxColumn cmbCol = new DataGridViewComboBoxColumn();  
  26.                    cmbCol.HeaderText = "Country";  
  27.                    cmbCol.Name = "Country2";  
  28.                    dataGridView1.Columns.Add(cmbCol);  
  29.                    dataGridView1.Columns["Country2"].DisplayIndex = 3;  
  30.   
  31.                    foreach (DataGridViewRow  row2 in dataGridView1.Rows)  
  32.                    {  
  33.                        row2.Cells["Country2"].Value = row2.Cells["Country"].Value;  
  34.   
  35.                        var dataSourceEn = new List<Country>();  
  36.                        dataSourceEn.Add(new Country() { Name = "SelectCountry", Value = 0 });  
  37.                        dataSourceEn.Add(new Country() { Name = "Jordon", Value = 1 });  
  38.                        dataSourceEn.Add(new Country() { Name = "Emarate ", Value = 2 });  
  39.                        dataSourceEn.Add(new Country() { Name = "Saudia", Value = 3 });  
  40.                        dataSourceEn.Add(new Country() { Name = "Bahreen", Value = 4 });  
  41.                        dataSourceEn.Add(new Country() { Name = "Tunisi", Value = 5 });  
  42.                        dataSourceEn.Add(new Country() { Name = "Algeri", Value = 6 });  
  43.                        dataSourceEn.Add(new Country() { Name = "Saudan", Value = 7 });  
  44.                        dataSourceEn.Add(new Country() { Name = "Syria", Value = 8 });  
  45.                        dataSourceEn.Add(new Country() { Name = "Eraq", Value = 9 });  
  46.                        dataSourceEn.Add(new Country() { Name = "Palestin", Value = 10 });  
  47.                        dataSourceEn.Add(new Country() { Name = "Qwit", Value = 11 });  
  48.                        dataSourceEn.Add(new Country() { Name = "Libanon", Value = 12 });  
  49.                        dataSourceEn.Add(new Country() { Name = "Libya", Value = 13 });  
  50.                        dataSourceEn.Add(new Country() { Name = "Egypt", Value = 14 });  
  51.                        dataSourceEn.Add(new Country() { Name = "Moroco", Value = 15 });  
  52.                        dataSourceEn.Add(new Country() { Name = "Mouritania", Value = 16 });  
  53.                        dataSourceEn.Add(new Country() { Name = "Yamen", Value = 17 });  
  54.                         
  55.                        cmbCol.DataSource = dataSourceEn;  
  56.                        cmbCol.DisplayMember = "Name";  
  57.                        cmbCol.ValueMember = "Value";  
  58.                      
  59.                    }  
  60.                    Save.Visible = true;  
  61.                   
  62.   
  63.   
  64.   
  65.   
  66.                }  
  67.            }  
  68.        }  
 
 

Answers (2)