DataGridView绑定后如何设置下拉框的索引,让它选中某一项?


DataGridView里有一ComboBox列,绑定后如何设置下拉框的索引,让它选中某一项?

DataGridViewComboBoxCell设置它的Value
DataGridViewComboBoxCell   cb   =   (DataGridViewComboBoxCell)dataGridView1.Rows[0].Cells[0];
cb.Value   =   "2 ";
前台加个模板列隐藏起来绑定ID

<asp:TemplateField   Visible= "False ">
                                                                        <ItemTemplate>
                                                                                <asp:CheckBox   ID= "CheckBox1 "   runat= "server "   />
                                                                                <asp:Label   ID= "MyName "   runat= "server "   Text= ' <%#Eval( "ID字段 ")   %> '> </asp:Label>
                                                                        </ItemTemplate>
                                                                </asp:TemplateField>

  <asp:ButtonField   ButtonType= "Button "   CausesValidation= "false "   HeaderText= "编辑列 "   Text= "编辑 "
                                                                        CommandName= "Select ">
                                                                        <ItemStyle   HorizontalAlign= "Center "   />
                                                                </asp:ButtonField>

                                                                <asp:ButtonField   ButtonType= "Button "   CausesValidation= "false "   HeaderText= "删除列 "   Text= "删除 "
                                                                        CommandName= "Delete ">
                                                                        <ItemStyle   HorizontalAlign= "Center "   />
                                                                </asp:ButtonField>
台直接在RowCommand事件里取
  protected   void   GridView1_RowCommand(object   sender,   GridViewCommandEventArgs   e)
        {
                if   (e.CommandName   ==   "Select ")
                {
                        int   index   =   Convert.ToInt32(e.CommandArgument);
                        Label   lbl   =   (Label)GridView1.Rows[index].FindControl( "MyName ");
                        ShowData(int.Parse(lbl.Text));
                }
                else   if   (e.CommandName   ==   "Delete ")
                {
                        int   index   =   Convert.ToInt32(e.CommandArgument);
                        Label   lbl   =   (Label)GridView1.Rows[index].FindControl( "MyName ");
                        col.Delete(int.Parse(lbl.Text));
                        PageLoad();
                }
        } 

  ShowData(int.Parse(lbl.Text));
  col.Delete(int.Parse(lbl.Text));
  PageLoad();
都是自己的方法..
int   index   =   Convert.ToInt32(e.CommandArgument);
Label   lbl   =   (Label)GridView1.Rows[index].FindControl( "MyName ");

lbl.text就可以取出ID值

转载于:https://www.cnblogs.com/bihailantian/archive/2010/09/21/1832628.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值