【问题标题】:How to Bind a column of GridView to a DropDownList?如何将 GridView 的一列绑定到 DropDownList?
【发布时间】:2013-03-06 09:01:37
【问题描述】:

我有一个 GridView,我想将第一个 Column(teachername) 与我在 GridView 之外的 DropDownList 绑定。我怎样才能让它工作?

 protected void Button2_Click(object sender, EventArgs e)
{
     string q = "select * from teacher where teachername='" + drpteachername.SelectedItem.ToString() + "'  and ('2013-03-01' between date and todate) and '2013-03-31' between date and todate";
    dt = dbo.Getdt(q);
    GridView1.DataSource = dt;
    GridView1.DataBind();
    string teachername=drpteachername.SelectedItem.ToString();


    for (int i = 0; i < dt.Rows.Count ;i++ )
    {
        Label lblteachername = (Label)GridView1.Rows[i].Cells[0].FindControl("lblgridteachername");

                  this.GridView1.Rows[i].Cells[0].Value = teachername;
    }


}

【问题讨论】:

  • 你有什么问题??
  • @huMptyduMpty 我点击按钮没有任何反应。上面的代码既没有给我任何结果也没有任何错误。
  • 我收到错误{“索引超出范围。必须为非负数且小于集合的大小。\r\n参数名称:索引”}

标签: c# asp.net


【解决方案1】:

尝试使用this.GridView1.Rows[i].Cells[0].Value = teachername;

如果您感到任何困惑,请不要犹豫写下。

此语句应嵌入 for 循环中。

【讨论】:

  • system.web.ui.webcontrols.tablecell' 不包含“值”的定义
  • 对我来说很好用。您使用的是哪个版本的 .NET?
  • 我正在使用 .NET 框架 4.0。我已经编辑了我的代码,看看我是否做得对。
  • 通过删除最后两行来检查,即 lblteachername.Text = teachername; GridView1.DataBind();这些值应在执行此代码时自动更改。
  • 我猜它的发生是因为 for (int i = 0; i
【解决方案2】:

您可以在预渲染事件中获取它。

protected void GridView1_PreRender(object sender, EventArgs e)
    {
        Label lblteachername = (Label)GridView1.Rows[i].FindControl("lblgridteachername");
         lblteachername.Text = teachername; 
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-24
    • 2014-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-24
    相关资源
    最近更新 更多