根据数据库设置DropDownList的选定值

时间:2016-07-21 15:51:21

标签: c# asp.net

我在gridview中有一个下拉列表,当文本框发生变化时,我希望下拉列表中的选定值(总共三个独立的值)与数据库中的数据相匹配。文本框中已更改事件的代码如下:

$time = str_pad($time,8,'00:',STR_PAD_LEFT)

1 个答案:

答案 0 :(得分:0)

尝试使用RowDataBound事件。要实现此目的,必须已使用值填充下拉列表,并且在这种情况下,将分配SelectedValue

protected void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        // this assumes the drop-down-list columns are the first, second, and third columns (ordinal positions 0, 1, and 2)
        DropDownList ddl1, ddl2, ddl3;
        ddl1 = (DropDownList)e.Row.Cells[0].Controls[0];
        ddl2 = (DropDownList)e.Row.Cells[1].Controls[0];
        ddl3 = (DropDownList)e.Row.Cells[2].Controls[0];
        DataRow currentRow = (DataRow)e.Row.DataItem;
        ddl1.SelectedValue = currentRow[0].ToString();
        ddl2.SelectedValue = currentRow[1].ToString();
        ddl3.SelectedValue = currentRow[2].ToString();
    }
}