在运行时选择DataGridViewCombobox单元格值 [英] Select DataGridViewCombobox cell value at runtime

查看:29
本文介绍了在运行时选择DataGridViewCombobox单元格值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,专家,

更改datagridviewcombobox单元格选择的任何想法在窗体加载时(即在运行时).DatagridViewCombobox单元格在设计时填充为Grant和Denied的数据.我想根据从数据库检索的参数显示已授予"或已拒绝".

注意:这是Windows应用程序.

在这方面的任何帮助将不胜感激.

问候,
Kartheesh

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

推荐答案

kartheesh,

瞧,有很多方法可以做到这一点.我将告诉您最简单的方法,假设您已将autogeneratecolumns设置为false.这意味着您正在使用<%#Eval("")%>或其他方法显示选定的字段.

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.

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



我认为这可以解决您的问题
Anurag



I think, this would solve your problem
Anurag


您应该听DataGridView.EditingControlShowing事件. e.Control可以转换为ComboBox控件:

this.myCachedComboBox =(ComboBox)e.Control;

在CellEndEdit事件中,您应该重置变量:

this.myCachedComboBox = null;

希望对您有所帮助,
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,


您好,Kartheesh,

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 :)


这篇关于在运行时选择DataGridViewCombobox单元格值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆