首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

gridVeiw 中的 dropdownlist 有关问题 急

2012-01-02 
gridVeiw 中的 dropdownlist 问题急~~~~我在gridView 中的模版列中放入dropDownList后台代码中:gridView.D

gridVeiw 中的 dropdownlist 问题 急~~~~
我在gridView 中的模版列中放入dropDownList
后台代码中:gridView.DataSource = dataTable1
我怎么才能让dropDownList的SelectedValue 的初值和dataTable1 中的那一列的值相同!!
高分跪求大侠指点!!


[解决办法]
友情up
[解决办法]
首先,你的dropDownList中应该已经有了值。

响应gridview的RowDataBound事件,首先判断Row的类型,如果是数据Row,则Find到dropdownlist控件对象,然后设置它的SelectedValue为DataBinder.Eval(e.Row.DataItem, "列名")

多说无意,随手写点代码:

C# code
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();    }}
[解决办法]
C# code
属性=<%#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


//下面一句至关重要,它会自动去绑定到dataTable的相应列上去
cmbColumn.DataPropertyName = "Col1"; // the property of the data source the DataGridView bounded

this.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; 
不知楼主还有什么疑问否?

热点排行