|
Hi all, I'm
using a Datagridviewcomboboxcolumn in to dsiplay some data in my
datagridview. After binding the data to the datagridviewcombobox how
can i make it display the first item in the box by default? My code is as follow DataGridViewComboBoxColumn col = new DataGridViewComboBoxColumn(); DataTable dt = getData();//get data from database col.DataSource = dt; col.DisplayMember = "LastName"; if (col.Items.Count > 0)//There is at least one item in the Combobox { //Select the first item } dataGridView1.Columns.Add(col); | |
Niradhip ChakrabortyRank #429 · 5.8K reputationPosted Jan 10, 2008, 11:11 AM EST
Hi u can do it following way.Apply the following code in datagrid click event.
private void dataGrid1_Click(object sender, System.EventArgs e)
{
DataGridCell currentCell;
string currentCellData;
// Get the current cell.
currentCell = DataGrid1.CurrentCell;
// Get the current cell's data.
currentCellData = (string)DataGrid1[currentCell.RowNumber,currentCell.ColumnNumber];
comboBox1.Items.Clear();
comboBox1.Items.Add (currentCellData);
}
abid zaid11 reputationPosted Jan 10, 2008, 12:29 PM EST
What I want is in the initialization. in other words when I add the datagridviewcombobox column,I want the first item to be selected. So that may have to deal of when adding a new row.
Niradhip ChakrabortyRank #429 · 5.8K reputationPosted Jan 10, 2008, 12:51 PM EST
abid zaid11 reputationPosted Jan 10, 2008, 2:46 PM EST
Niradhip ChakrabortyRank #429 · 5.8K reputationPosted Jan 18, 2008, 12:56 PM EST
Pablo Cicchino2 reputationPosted Feb 16, 2008, 3:23 AM EST
stephennoronha2 reputationPosted Jun 10, 2008, 5:07 AM EDT
Hi,
Did anyone get a solution for this? I also want a default item selected in the dropdownlist. here is my code:
private void bindGrid()
{
myGridView = AddColumnsToGrid(myGridView);
-- here I want to get the default selected.... I have no idea how ---
}
private DataGridView AddColumnsToGrid(DataGridView myGridView)
{
DataGridViewComboBoxColumn m_CColumn =
new DataGridViewComboBoxColumn();
m_CColumn.DataSource = _dsDeptName.Tables[0]; <- Binding this to a datasource
m_CColumn.HeaderText = "DEPT NAME";
m_CColumn.DisplayMember = "DEPARTMENT_NAME";
m_CColumn.ValueMember = "DEPARTMENT_NAME";
myGridView.Columns.Add(m_CColumn);
return myGridView;
}
abid zaid11 reputationPosted Jun 12, 2008, 9:55 PM EDT
cl.Items.Add("One");
cl.Items.Add("Two");
cl.Items.Add("Three");
dataGridView1.Columns.Add(cl);
dataGridView1.Rows[0].Cells[1].Value ="Three";
abid zaid11 reputationPosted Jun 12, 2008, 9:55 PM EDT
cl.Items.Add("One");
cl.Items.Add("Two");
cl.Items.Add("Three");
dataGridView1.Columns.Add(cl);
dataGridView1.Rows[0].Cells[1].Value ="Three";
abid zaid11 reputationPosted Jun 12, 2008, 9:57 PM EDT
cl.Items.Add("One");
cl.Items.Add("Two");
cl.Items.Add("Three");
dataGridView1.Columns.Add(cl);
dataGridView1.Rows[0].Cells[0].Value ="Three";
This part indicate the row and column of your datagridviewcomboboxcolumncell