
Even if you have your data in your dataTable, instead of binding the DataTable to DataGridView, create the DataGridview cell by cell by iterating the dataTable values. In which ever cell you need a combobox, make it a DataGridViewComboBoxCell, in rest of the cells, use ordinary DataGridViewTextBoxCell. Make sure you populate the comboBox from some source like DataTable or list. The code below shows how to create a DataGridView as show in the image on leftside. Dont worry about the condition statements, it is spicific to the requirement of my program.
First create the columns of the DataGridView programmatically.
foreach (DataColumn column in ds11.Tables[0].Columns)
{
dgdProcessedGrid.Columns.Add(column.ColumnName,column.ColumnName);
dgdProcessedGrid.Columns[column.ColumnName].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
if (FirstColumn) FirstColumn = false;
else dgdProcessedGrid.Columns[column.ColumnName].Visible = false;
}
After difining the columns of the DataGridView, the code below creates and inserts the value for each and every cell of DataGridView
#region CreatingCellsNRows
foreach (DataRow row in ds.Tables[0].Rows)
{
DataGridViewRow dataGridRow = new DataGridViewRow();
int ColumnCount = 0;
foreach (var str in row.ItemArray)
{
bool ToValidateColumn = false;
foreach(DataColumn column in ds12.Tables[0].Columns )
{
if (string.Equals(ds.Tables[0].Columns[ColumnCount].ColumnName.ToString(), column.ColumnName) == true)
{
ToValidateColumn = true;
dgdProcessedGrid.Columns[column.ColumnName].Visible = true;
break;
}
}
if (ToValidateColumn && str.ToString().Contains("<") == true str.ToString().Contains(".01") == true) { DataGridViewCell[] cells = new DataGridViewCell[1]; DataGridViewComboBoxCell cmb = new DataGridViewComboBoxCell(); List
var varDistinct1 = (from data in ds12.Tables[0].AsEnumerable()
select data[ds11.Tables[0].Columns[ColumnCount].ColumnName.ToString()].ToString()).Distinct();
if (varDistinct1 != null && varDistinct1.Count() > 0)
{
foreach (string sColumnValue in varDistinct1)
{
if (!string.IsNullOrEmpty(sColumnValue))
ilMAList1.Add(sColumnValue);
}
ilMAList1.TrimExcess();
ilMAList1.Add(str.ToString());
cmb.DataSource = ilMAList1;
cmb.Value = str.ToString();
dataGridRow.Cells.Add(cmb);
}
}
else
{
DataGridViewCell[] cells = new DataGridViewCell[1];
DataGridViewTextBoxCell txt1A = new DataGridViewTextBoxCell();
txt1A.Value = str;
dataGridRow.Cells.Add(txt1A);
txt1A.ReadOnly = true;
}
ColumnCount++;
}
dgdProcessedGrid.Rows.Add(dataGridRow);
}
#endregion CreatingCellsNRows