我在gridView 中的模版列中放入dropDownList
后台代码中:gridView.DataSource = dataTable1
我怎么才能让dropDownList的SelectedValue 的初值和dataTable1 中的那一列的值相同!!
高分跪求大侠指点!!
后台代码中:gridView.DataSource = dataTable1
我怎么才能让dropDownList的SelectedValue 的初值和dataTable1 中的那一列的值相同!!
高分跪求大侠指点!!
{
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();
}}
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;
}
}
}
{
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
//创建一个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
//下面一句至关重要,它会自动去绑定到dataTable的相应列上去
cmbColumn.DataPropertyName = "Col1"; // the property of the data source the DataGridView boundedthis.dataGridView1.Columns.Add(cmbColumn);
this.dataGridView1.Columns[0].HeaderText = "Col1";dataGridView1.DataSource = dt;
前台属性绑定。
后台动态设置也有两种方法:
1.ddl.SelectedValue = DataBinder.Eval(e.Row.DataItem, "列名").ToString();
2.ddl.Items.FindByValue(drv["ClassID"].ToString()).Selected = true; ///这是根据VALUE查找
3.ddl.Items.FindByText(drv["ClassName"].ToString()).Selected = true;
不知楼主还有什么疑问否?