How load specific value DataGridViewComboBoxColumn - c#

Hi all I have binded my datagridview with DataGridViewComboBoxColumn as follows
dataGridView1.DataSource = db.DT(sql);
DataGridViewComboBoxColumn Column1 = new DataGridViewComboBoxColumn();
Column1.HeaderText = "Status";
Column1.Name = "Column1";
DTchequeStatus = db.DT("SELECT [ID] ,[Status] FROM [ChequeStatus]");
Column1.DisplayMember = "Status";
Column1.ValueMember = "ID";
Column1.DataSource = DTchequeStatus;
foreach (DataRow DR in DTchequeStatus.Rows)
{
// Column1.Items.Add(DR["Status"].ToString());
}
this.dataGridView1.Columns.Add(Column1);
dataGridView1.Columns["Column1"].ReadOnly = false;
foreach (DataGridViewRow DR in dataGridView1.Rows)
{
(DR.Cells["Column1"] as DataGridViewComboBoxCell).Value = (int)DR.Cells["Cheque Status"].Value;
}
But this is resulting in an empty value for my datagridview as follows. What should I do inorder to get the one value selected i tried this Why on Initial load DataGridViewComboBoxColumn showing empty value but doesn't works

Just setting
Column1.DataPropertyName = "ID"
will resolved the problem

Related

Can't change the default value of checkbox in DataGridView (Windows Form Application)

I'm trying to check checkboxes for all rows as default in DataGridView. But the code below is not working.
DataTable dt = new DataTable();
sqlDA.Fill(dt);
dgvTeacherClasses.DataSource = dt;
DataGridViewCheckBoxColumn checkCell = new DataGridViewCheckBoxColumn
{
ValueType = typeof(bool),
Name = "check",
HeaderText = ""
};
dgvTeacherClasses.Columns.Add(checkCell);
foreach (DataGridViewRow row in dgvTeacherClasses.Rows)
{
row.Cells[checkCell.Name].Value = true;
}
Checkboxes are still remain unchecked. Any idea how to fix this?
Solution is here: https://stackoverflow.com/a/32365172/6664548
DataTable dt = new DataTable();
sqlDA.Fill(dt);
dgvTeacherClasses.DataSource = dt;
dt.Columns.Add("checkCell", typeof(bool));
foreach (DataGridViewRow row in dgvTeacherClasses.Rows)
{
row.Cells["checkCell"].Value = true;
}

Datagridview check box checked is not working

I added checkbox in datagridview and trying to put default value as checked but it is showing checkbox unchecked, here is my code:
OracleDataAdapter da = new OracleDataAdapter(qry, myConnection);
OracleCommandBuilder cmdBuilder = new OracleCommandBuilder(da);
DataSet ds = new DataSet();
da.Fill(ds);
dgv.DataSource = ds.Tables[0];
DataGridViewCheckBoxColumn checkColumn = new DataGridViewCheckBoxColumn();
checkColumn.Name = "X";
checkColumn.HeaderText = "X";
checkColumn.Width = 50;
checkColumn.ReadOnly = false;
dgv.Columns.Insert(8, checkColumn);
foreach (DataGridViewRow row in dgv.Rows)
{
DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)row.Cells[dgv.Columns.Count - 1];
chk.Value = chk.TrueValue;
}
I tried the following code also but still not showing as "CHECKED":
chk.Value = true;
Please help me solve this issue, thanks
default value of DataGridViewCheckBoxCell.TrueValue is null. Set the TrueValue property of the owning column:
checkColumn.TrueValue = true;

How can you add combobox to datagridview from datatable?

Here is a code where I add value to datagridview from datatable. The "First" and "Third" column of the datagridview have been filled with data from the datatable. The problem is for the "Second" and the "Forth" column, as I have to make it a combobox for the user to choose. Each combobox has default value which is "columnDefaultValue".
string sqlMatchedData = "SELECT colA, colB, colC, colD " +
"FROM TB_LOOKUP_COLUMN "
ds = databaseManager.GetData(sqlMatchedData);
dataGridView1.ColumnCount = 4;
dataGridView1.Columns[0].Name = "First";
dataGridView1.Columns[1].Name = "Second";
dataGridView1.Columns[2].Name = "Third";
dataGridView1.Columns[3].Name = "Forth";
foreach(DataRow row in ds.Tables[0].Rows)
{
int n = dataGridView1.Rows.Add();
dataGridView1.Rows[n].Cells[0].Value = row[0].ToString();
dataGridView1.Rows[n].Cells[2].Value = row[3].ToString();
string columnDestination = row[1].ToString();
string columnType = row[2].ToString();
comboboxDestinationColumn(columnDefaultValue);
}
How can I create combobox using the datatable and bind it to the specific cell?
private void comboboxDestinationColumn(string columnDefaultValue)
{
string sqlLookupColumn = "SELECT colALookUp, colBLookUp FROM TB_LOOKUP_COLUMN";
DataSet dsColumn = databaseManager.GetData(sqlLookupColumn);
DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn();
combo.HeaderText = "Destination";
combo.Name = "combo";
foreach(DataRow row in dsColumn.Tables[0].Rows)
{
//populate combobox with data from datatable with defaul value columnDefaultValue
}
}
EDITED
I have found the way to add the combobox to datagridview. But i still lacking the way on how can i set the default value for each combobox based on the variable columnDefaultValue
private void comboboxDestinationColumn(string columnDefaultValue, int n)
{
string sqlLookupColumn = "SELECT colALookUp, colBLookUp FROM TB_LOOKUP_COLUMN";
DataSet dsColumn = databaseManager.GetData(sqlLookupColumn);
DataGridViewComboBoxCell comboboxColumn = new DataGridViewComboBoxCell();
foreach (DataRow row in dsColumn.Tables[0].Rows)
{
comboboxColumn.Items.Add(row[1].ToString());
}
dataGridView1.Rows[n].Cells[1] = comboboxColumn;
}
I'm trying to find something like:
comboboxColumn.Selected = true;
What is the proper way to do this?
You couls just set a value of your comboboxcell after creating it. Like after the foreach loop, you should just put comboboxColumn.Value = comboboxColumn.Items[0] //or whichever of all the items you want. That would give your combobox the wanted value.
P.S.: It's a bad practic to name a DataGridViewComboBoxCell like "comboboxColumn"...

Why are DataTable values not showing on WPF DataGrid when column name contains '/'?

The name of the second column in the DataTable below is "f/g", and when I run it I don't see the values in the second column on screen, why is that?
btw, when I change it into "f:g" it works, but "f[g" does not.
DataTable resultDataTable = new DataTable();
DataColumn id_column = new DataColumn();
id_column.DataType = System.Type.GetType("System.String");
id_column.ColumnName = "ID";
id_column.ReadOnly = true;
id_column.Unique = true;
resultDataTable.Columns.Add(id_column);
DataColumn f_column = new DataColumn();
f_column.DataType = System.Type.GetType("System.Double");
f_column.ColumnName = "f/g";
f_column.ReadOnly = true;
resultDataTable.Columns.Add(f_column);
foreach (var entry in results.Results)
{
DataRow dr = resultDataTable.NewRow();
dr[0] = entry.Key;
dr[1] = entry.Value;
resultDataTable.Rows.Add(dr);
}
ResultsDataGrid.ItemsSource = resultDataTable.AsDataView();
OK, I don't know why its not working but I did find a way to workaround it by using the Column Caption property and implementing the AutoGeneratingColumn event in the DataGrid:
private void ResultsDataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
var dGrid = (sender as DataGrid);
if (dGrid == null) return;
var view = dGrid.ItemsSource as DataView;
if (view == null) return;
var table = view.Table;
e.Column.Header = table.Columns[e.Column.Header as String].Caption;
}
Basically it take whatever is in the Caption property and put it in the DataGrid Column Header even "f/g".
Try changing
"f/g"
to
"f//g" or f[/]g
Escape the character

how to set value in DataGridViewComboBoxColumn from a datatable?

DataGridViewComboBoxColumn dgvcb = (DataGridViewComboBoxColumn)grvPackList.Columns["Units"];
Globals.G_ProductUtility G_Utility = new Globals.G_ProductUtility();
dgvcb.DisplayStyle = DataGridViewComboBoxDisplayStyle.DropDownButton;
G_Utility.addUnittoComboDGV(dgvcb);
DataSet _ds = iRawMaterialsRequest.Select();
grvPackList.DataSource = _ds.Tables[0];
the problem is that the DataGridViewComboBoxColumn in the datagrid is not selected with the value in table how is it possilbe to set value of DataGridViewComboBoxColumn from datasoure
int i=0;
foreach (DataRow dgvr in _ds.Tables[0].Rows )
{
grvPackList.Rows[i].Cells["Units"].Value = dgvr["Units"].ToString();
i++;
}
this code is working but is there any solution with out using loops?
int i=0;
foreach (DataRow dgvr in _ds.Tables[0].Rows )
{
grvPackList.Rows[i].Cells["Units"].Value = dgvr["Units"].ToString();
i++;
}
When i Tried this it worked fine
you can bind the DataGridViewComboBoxColumn directly to your datasouce like
DataGridViewComboBoxColumn dgvcb = (DataGridViewComboBoxColumn)grvPackList.Columns["Units"];
dgvcb.ValueMember = "YourUnitValue";
dgvcb.DisplayMember = "Units";
dgvcb.DataSource = _ds.Tables[0];

Categories

Resources