Click here to Skip to main content
15,890,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Experts,

Any idea to change the selection of datagridviewcombobox cell On form load(i.e at runtime).DatagridViewCombobox cell is filled with data as Granted and Denied at design time. I wanted to display Granted or Denied based on parameters retrieved from database.

Note: This is windows application.

Any help in this regard would be appreciated.

Regards,
Kartheesh
Posted
Updated 16-Sep-10 23:32pm
v4
Comments
Dalek Dave 17-Sep-10 3:25am    
Edited for Grammar and Readability.
T.Saravanann 17-Sep-10 5:11am    
Are you working windows or web application
kartheesh 17-Sep-10 5:16am    
windows application

Hi kartheesh,

See, there are many ways of doing it. I am going to tell you the simplest method assuming that you have put autogeneratecolumns to false. This means that you are showing selected fields using <%#Eval("")%> or some other method.

XML
<asp:DropDownList ID="ddl1" runat="server" DataSourceID="SqlDataSource1" DataValueField="Status" AutoPostBack="True" SelectedValue='<%# Convert.ToInt32(Eval("Denied")) == 1? "1":"0" %>' >
    </asp:DropDownList>


I think, this would solve your problem
Anurag
 
Share this answer
 
v2
You should listen to the DataGridView.EditingControlShowing event. e.Control can be casted to a ComboBox control:

this.myCachedComboBox = (ComboBox) e.Control;

In the CellEndEdit event, you should reset your variable:

this.myCachedComboBox = null;

Hope this helps,
 
Share this answer
 
Comments
Henry Minute 17-Sep-10 6:29am    
I do not believe that this answers the question being asked.
Hi Kartheesh,

// Create ComboBox Column in DataGridView
DataGridViewComboBoxColumn dgvcmb=new DataGridViewComboBoxColumn();
dgvcmb.DisplayMember = "Name";
dgvcmb.ValueMember= "Id";
dgvcmb.DataSource= dtData.Copy();
dataGridView1.Controls.Add(dgvcmb);

// Get Selected Value from that ComboBox
private void dataGridView1_EditingControlShowing(object sender,
  DataGridViewEditingControlShowingEventArgs e)
  {
  if (dataGridView1.CurrentCell.ColumnIndex >= 0)
  {    
      ComboBox cmbBox= e.Control as ComboBox;
      cmbBox.SelectedIndexChanged += new EventHandler(comboBox_SelectedIndexChanged);
 }
  }

  void comboBox_SelectedIndexChanged(object sender, EventArgs e)
  {
   if(((ComboBox)sender).SelectedValue != null)
   {
    string sId =((ComboBox)sender).SelectedValue.ToString(); 
    string sName =((ComboBox)sender).Text.ToString();
   }
  }



Refer this code,its will helpful to you.

Cheers :)
 
Share this answer
 
v3
Comments
Henry Minute 17-Sep-10 6:30am    
I do not believe that this answers the question being asked.
RAMALINGAM.K 28-Mar-12 3:53am    
but i have one problems .... i am added datagridview combobox in design side ...so here added additional row added autometically
Henry Minute 28-Mar-12 4:18am    
I think that you have posted your message in the wrong place.
On the onload event, get the values from the database. Assign the value directly to the datagridviewcell (assuming it's not databound). If it's one of the values you specified at design time, it should automatically adjust the selection.

If granted or denied is represented as 'G' and 'D' in the database. Then cast the datagridviewcell into a datagridviewcombobox and change the selected index accordingly.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900