日期:2014-05-18  浏览次数:20896 次

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