Deeraj S

Deeraj S

  • NA
  • 145
  • 12.4k

How to bind different items to each cells of combobox column in datagr

Sep 23 2020 4:29 AM
How to bind different items to each row cell of particular combobox column in datagridview

Answers (1)

2
Rajanikant Hawaldar

Rajanikant Hawaldar

  • 32
  • 38.8k
  • 438.6k
Sep 23 2020 4:43 AM
hello deerj here is sample code,
  1. using System.Windows.Forms;  
  2.   
  3. namespace BindDifferentItemsInEachCellofGridWithComboType  
  4. {  
  5.     public partial class Form1 : Form  
  6.     {  
  7.         private DataGridView DataGridViewObj = new DataGridView();  
  8.         public Form1()  
  9.         {  
  10.             InitializeComponent();  
  11.   
  12.             DataGridViewComboBoxColumn dataGridViewComboBoxColumn = new DataGridViewComboBoxColumn();  
  13.             dataGridViewComboBoxColumn.HeaderText = "Different Items Combobox";  
  14.             DataGridViewObj.Columns.Add(dataGridViewComboBoxColumn);  
  15.             this.Controls.Add(DataGridViewObj);  
  16.             //Binding combobox particuler cell  
  17.             setCellComboBoxItems(DataGridViewObj,0,0,new object[] { "C#""Python""VB" });  
  18.             DataGridViewObj.Rows.Add();  
  19.             setCellComboBoxItems(DataGridViewObj,0,0,new object[] { "PHP""C++" });  
  20.         }  
  21.       
  22.         private void setCellComboBoxItems(DataGridView dataGridViewObj, int rowIndex, int colIndex, object[] itemsToBindCell)  
  23.         {  
  24.             DataGridViewComboBoxCell dgvcbCell = (DataGridViewComboBoxCell)dataGridViewObj.Rows[rowIndex].Cells[colIndex];  
  25.             dgvcbCell.Items.Clear();  
  26.             foreach (object itemToAdd in itemsToBindCell)  
  27.             {  
  28.                 dgvcbCell.Items.Add(itemToAdd);  
  29.             }  
  30.         }  
  31.     }  
  32. }  
Accepted Answer