日期:2014-05-18 浏览次数:20896 次
void gridView_RowDataBound(object sender, GridViewRowEventArgs e) { if(e.Row.RowType == DataControlRowType.DataRow) { DropDownList ddl = (DropDownList)e.Row.Cells[x].FindControl(dropDownList); // Cells[x]就是包含了dropdownlist控件的哪个列 ddl.SelectedValue = DataBinder.Eval(e.Row.DataItem, "列名").ToString(); // 或者: // DataRowView drv = (DataRowView)e.Row.DataItem; // ddl.SelectedValue = drv["列名"].ToString(); } }
------解决方案--------------------
属性=<%#Eval("catalogue")%>
------解决方案--------------------
给你个参考:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
System.Data.DataRowView drv = (System.Data.DataRowView)e.Row.DataItem; RadioButtonList rbl = (RadioButtonList)e.Row.FindControl("txtGender");
if (rbl != null)
{
if ((bool)drv["Gender"])
{
rbl.Items.FindByText("男").Selected = true;
}
else
{
rbl.Items.FindByText("女").Selected = true;
}
}
DropDownList ddl = (DropDownList)e.Row.FindControl("txtClassName");
if (ddl != null)
{
ddl.Items.FindByText(drv["ClassName"].ToString()).Selected = true;
}
}
}
------解决方案--------------------
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
System.Data.DataRowView drv = (System.Data.DataRowView)e.Row.DataItem; RadioButtonList rbl = (RadioButtonList)e.Row.FindControl("txtGender");
if (rbl != null)
{
if ((bool)drv["Gender"])
{
rbl.Items.FindByText("男").Selected = true;
}
else
{
rbl.Items.FindByText("女").Selected = true;
}
}
DropDownList ddl = (DropDownList)e.Row.FindControl("txtClassName");
if (ddl != null)
{
ddl.Items.FindByValue(drv["ClassID"].ToString()).Selected = true; ///这是根据VALUE查找
}
}
}
请参考:ListItemCollection.FindByText(FindByValue) Method
------解决方案--------------------
我写过ComboBox的例子,和DropDownList差不多的,你借鉴一下:
//创建一个DataTable用于数据源
DataTable dt = new DataTable("TestTable");
dt.Columns.Add("Col1", typeof(int));
dt.Columns.Add("Col2", typeof(string));
dt.Rows.Add(1, "aaa");
dt.Rows.Add(2, "bbb");
//绑定的过程
DataGridViewComboBoxColumn cmbColumn = new DataGridViewComboBoxColumn();
cmbColumn.DataSource = dt; // bound ComboBox to a data source
cmbColumn.DisplayMember = "Col1"; //the property of the data source for the ComboBox to show