【问题标题】:How can I update a DataGrid Column with the selectedvalue of a Dropdownlist?如何使用下拉列表的选定值更新 DataGrid 列?
【发布时间】:2013-10-30 14:35:23
【问题描述】:

我正在使用 C# Web 应用程序。我有一个包含 4 列的 DataGrid。三列是标签,一列是 DropDownList。在我的 ddl_SelectedIndexChanged 中,我希望能够从我的 DDL(单元格 [3])中获取选定的值并将其放在同一行的单元格 [2] 中。

这就是我所拥有的:

protected void ddlMachId_SelectedIndexChanged(object sender, EventArgs e)
{
    DropDownList ddl = (DropDownList)sender;

    string val = ddl.SelectedValue.ToString();

    TableCell cell = ddl.Parent as TableCell;
    DataGridItem item = cell.Parent as DataGridItem;

    item.Cells[2] = val;
}  

这给了我一个错误,说 item.Cells[2] 是只读的。

有什么想法吗?谢谢。

【问题讨论】:

    标签: c# asp.net .net datagrid html-select


    【解决方案1】:

    请尝试一下:

    //Get the actual row, and key for the column...
    row.Cells[2].Value = val;
    

    【讨论】:

    • 那是什么?我在那个程序中没有一行,什么是星星?
    • 更新,该行可以从datagrid实际选中的行属性中获取
    【解决方案2】:

    如果您使用的是 DataGrid,请执行以下操作:

    protected void ddlMachId_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList list = (DropDownList)sender;    
        TableCell cell = list.Parent as TableCell;
        DataGridItem item = cell.Parent as DataGridItem;
    
        string val = list.SelectedValue.ToString();
        item.Cells[2].Text = val; 
    }
    

    如果你使用GridView,你必须使用namingcontainer来获取gridview行:

    protected void ddlMachId_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddl = (DropDownList)sender;
        GridViewRow row = (GridViewRow)ddl.NamingContainer;
    
        string val = ddl.SelectedValue.ToString();
        row.Cells[2].Text = val;
    } 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-03
      • 1970-01-01
      • 1970-01-01
      • 2023-03-13
      • 1970-01-01
      • 2015-05-12
      • 2013-11-07
      • 2014-09-08
      相关资源
      最近更新 更多