I have DataGridViewComboBoxColumn in my datagridview and I want to populate using linq in run-time:
I try this
var line = from li in dbdata.Production_lines select li;
grdEmp.Rows.Add();
(grdEmp.Rows[count].Cells["Line"] as DataGridViewComboBoxCell).DataSource = line;
(grdEmp.Rows[count].Cells["Line"] as DataGridViewComboBoxCell).ValueMember = "L_id";
(grdEmp.Rows[count].Cells["Line"] as DataGridViewComboBoxCell).DisplayMember = "L_id";
But this didn't work. so how do I bind the data to ComboBox in my DataGridView
Try this :
DataGridViewComboBoxColumn combo = (DataGridViewComboBoxColumn)grdEmp.Columns["Line"];
combo.DataSource = line;
combo.ValueMember = "L_id";
combo.DisplayMember = "L_id"; // I also suspect this should be a text column
Related
I want to insert a combobox for each row in datagrid, but when I tried to send a combobox for specific cell it doesn't work
for (int i = 0; i < dtRecord.Rows.Count; i++)
{
int idRecord = Convert.ToInt32(dtRecord.Rows[i]["idRecord"].ToString());
DataTable dtDetalleRecordPorId = new DataTable();
dtDetalleRecordPorId = cnEvalua.CNListaDetalleRecord(idRecord);
DataGridViewComboBoxCell cmb = new DataGridViewComboBoxCell
{
DataSource = dtDetalleRecordPorId,
ValueMember = dtDetalleRecordPorId.Columns["idDetalleRecord"].ToString(),
DisplayMember = dtDetalleRecordPorId.Columns["cValor"].ToString(),
ReadOnly = false
};
dtgRecord.Rows[i].Cells["cValor"] = cmb;
}
When I debug and check datasource value of datagrid is empty. How can I insert that combobox?
My datagrid take data from this part:
dtRecord = cnEvalua.CNListaRecord();
dtgRecord.DataSource = dtRecord;
When I check datasource of gridview the column cValor is empty but other columns have data.
You can try this,
DataGridViewComboBoxColumn cmb = new DataGridViewComboBoxColumn();
cmb.DataSource = dtDetalleRecordPorId;
cmb.ValueMember = dtDetalleRecordPorId.Columns["idDetalleRecord"].ToString();
cmb.DisplayMember = dtDetalleRecordPorId.Columns["cValor"].ToString();
cmb.ReadOnly = false;
dtgRecord.Columns.Add(cmb);
I am binding DataGridViewComboBoxColumn to DataTable, but the the cells of the column are not displaying the bound table.
any idea?
to test the DataTable, I bound it to a normal ComboBox and this shows the expected behavior.
private void populateDataGW()
{
int addressesCount = data.Length / 186;
TestingAdrress[] addressArray = new TestingAdrress[addressesCount];
addressArray = getArray();
DataGridViewRow dtRow = new DataGridViewRow();
dtTableCmbBx = getAllCustomers();
DataGridViewComboBoxColumn cmBxCol = new DataGridViewComboBoxColumn();
//Binding here is not working
cmBxCol.DataSource = dtTableCmbBx;
cmBxCol.ValueMember = "Customer_ID";
cmBxCol.DisplayMember = "Name";
dtGrViTestAddress.Columns.Add(cmBxCol);
//code for "normal" combobox
comboBox1.DataSource = dtTableCmbBx;
comboBox1.DisplayMember= "Name";
comboBox1.ValueMember = "Customer_ID";
DataGridViewButtonCell btnCell = new DataGridViewButtonCell();
btnCell.Value = "Hinzufügen";
for (int i=0; i< addressArray.Length;i++)
{
dtGrViTestAddress.Rows.Add();
dtGrViTestAddress.Rows[i].Cells[0].Value = addressArray[i].Name;
dtGrViTestAddress.Rows[i].Cells[1].Value = addressArray[i].Street;
dtGrViTestAddress.Rows[i].Cells[2].Value = addressArray[i].PostalCode;
dtGrViTestAddress.Rows[i].Cells[3].Value = addressArray[i].City;
dtGrViTestAddress.Rows[i].Cells[5] = btnCell;
}
}
Here is a screenshot:
i found the problem. i wanted the datagridview to be readOnly, so thats why the combobox didnt show the items in it, now i changed this property, and i set it for each column separated, and let the comboboxcolumn to be readOnly=false
Method 1: Use Linq
var details = (from x in db.Details
orderby x.Datetime descending
where x.RaisedBy == "xyz"
select x).ToList();
comboBox1.ValueMember ="id";
comboBox1.DataSource = details;
comboBox2.ValueMember ="Name";
comboBox2.DataSource = details;
comboBox3.ValueMember ="Street";
comboBox3.DataSource = details;
comboBox4.ValueMember ="PostalCode";
comboBox4.DataSource = details;
Method :2
var combocolumnA = new DataGridViewComboBoxColumn();
combocolumnA.HeaderText = "ID"; // grid header name
combocolumnA.ValueMember = "id";// database Column name
combocolumnA.DataSource = details;
GV.Columns.Add(combocolumnA);
combocolumnA.Width = 100;
var combocolumnB = new DataGridViewComboBoxColumn();
combocolumnB.HeaderText = "Name";
combocolumnB.ValueMember = "Name";
combocolumnB.DataSource = details;
GV.Columns.Add(combocolumnB);
combocolumnB.Width = 150;
Edit:
Just to tell you one more thing... If you want the data in grid view suppose when you select name from combo box then data automatically change all fields of gridview according to database ?
I have a ComboBox with its DataSource set to an instance of a DataTable. When rows are added to the DataTable, they show in the ComboBox without additional code, but when a row is deleted, the ComboBox remains unchanged. A short summary of my code:
ComboBox selector = new ComboBox();
DataTable tbl = new DataTable();
PopulateTable()
{
DataRow row1 = tbl.NewRow();
row1["field1"] = 1;
row1["field2"] = "Some Text";
tbl.Rows.Add(row1);
DataRow row2 = tbl.NewRow();
row2["field1"] = 2;
row2["field2"] = "More Text";
tbl.Rows.Add(row2);
}
PopulateSelector()
{
selector.DisplayMember = "field2";
selector.ValueMember = "field1";
selector.DataSource = tbl;
}
RemoveRow()
{
tbl.Rows[0].Delete();
}
At this point, the ComboBox appears to be correct, but clicking it resets it to its previous data. The DataTable remains correct, deleting the row causes no problem in that instance, I just can't make the ComboBox reflect the changes.
Try this:
PopulateSelector()
{
selector.DataSource = null;
selector.DisplayMember = "field2";
selector.ValueMember = "field1";
selector.DataSource = tbl;
}
RemoveRow()
{
tbl.Rows[0].Delete();
PopulateSelector()
}
You can use a bindingsource inbetween the datatable and the combobox and call ResetBindings
hi i runtime bind the data into datagridview combobox. But how do i make it to auto display the first item? i do not able find the selectedindex from DataGridViewComboBoxColumn.
DataGridViewComboBoxColumn cbStudentCourse = (DataGridViewComboBoxColumn)dgStudentCourse.Columns["studentCourseStatus"];
cbStudentCourse.DataSource = Enum.GetValues(typeof(CourseStudentStatus));
cbStudentCourse.DisplayIndex = 1;
-- Update ---
i saw someone doing this in solution 3
LInk
Are you sure i need such a long code to just have the first item selected??????
A DataGridViewComboBoxColumn has no SelectedIndex, and SelectedValue properties. However you can get the same behavior of SelectedValue by setting the Value property.
For instance on first index the value member has value 2 then you should set .Value = "2" to set the first index selected.
For example
myDataGridViewComboBoxColumn.Value = "20";
In your case
myDataGridViewComboBoxColumn.Value = CourseStudentStatus.EnumToBeSelected.ToString();
Here is more details about DataGridViewComboBoxColumn
the best way to set the value of a datagridViewComboBoxCell is:
DataTable dt = new DataTable();
dt.Columns.Add("Item");
dt.Columns.Add("Value");
dt.Rows.Add("Item1", "0");
dt.Rows.Add("Item1", "1");
dt.Rows.Add("Item1", "2");
dt.Rows.Add("Item1", "3");
DataGridViewComboBoxColumn cmb = new DataGridViewComboBoxColumn();
cmb.DefaultCellStyle.Font = new Font("Tahoma", 8, FontStyle.Bold);
cmb.DefaultCellStyle.ForeColor = Color.BlueViolet;
cmb.FlatStyle = FlatStyle.Flat;
cmb.Name = "ComboColumnSample";
cmb.HeaderText = "ComboColumnSample";
cmb.DisplayMember = "Item";
cmb.ValueMember = "Value";
DatagridView dvg=new DataGridView();
dvg.Columns.Add(cmb);
cmb.DataSource = dt;
for (int i = 0; i < dvg.Rows.Count; i++)
{
dvg.Rows[i].Cells["ComboColumnSample"].Value = (cmb.Items[0] as
DataRowView).Row[1].ToString();
}
It worked with me very well
I am trying to create a gridview with a string column, a checkbox column, and a dropdownlist/combobox column. The first two are finished (all code behind), just need help with the last one.
DataTable dt = new DataTable("tblAir");
dt.Columns.Add("Flight Details", typeof(string));
dt.Columns.Add("Prefered Seating", typeof(bool));
//doesn't work
dt.Columns.Add("Add Remark", typeof(ComboBox));
The data for the combobox is being supplied on load as we cannot work with a database.
Peter Bromberg has a detailed article on creating a Winforms gridview with comboboxes:
http://www.eggheadcafe.com/articles/20060202.asp
DataAccessLayer dal = new DataAccessLayer();
DataTable movies = dal.GetMovies();
gvMovies.DataSource = movies;
gvMovies.AllowUserToAddRows = false;
gvMovies.AllowUserToDeleteRows = false;
//Create the new combobox column and set it's DataSource to a DataTable
DataGridViewComboBoxColumn col = new DataGridViewComboBoxColumn();
col.DataSource = dal.GetMovieTypes(); ;
col.ValueMember = "MovieTypeID";
col.DisplayMember = "MovieType";
col.DataPropertyName = "MovieTypeID";
//Add your new combobox column to the gridview
gvMovies.Columns.Add(col);