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值