【问题标题】:Getting the value of Full-Row Clickable Gridview and Display in Textbox获取全行可点击网格视图的值并在文本框中显示
【发布时间】:2015-04-16 09:49:30
【问题描述】:

我有一个从存储过程加载数据的gridview。

目标:

  1. 通过在 gridview 中隐藏选择列来使整个 gridview 行可点击 - 我可以通过在 RowDataBound 事件中执行以下代码来做到这一点。

  2. 接下来,当我单击网格视图中的整行时,我应该能够从所选行中获取数据/值,并将其显示在弹出模式的文本框中。这是用于更新/编辑功能。

这就是我得到的东西

 protected void grdTenant_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        e.Row.Cells[0].Style["display"] = "none";

        if (e.Row.RowType == DataControlRowType.DataRow)
        { 

            e.Row.Attributes["onmouseover"] =
                "javascript:setMouseOverColor(this);";
            e.Row.Attributes["onmouseout"] =
                "javascript:setMouseOutColor(this);";
            e.Row.Attributes["onclick"] =
            Page.ClientScript.GetPostBackClientHyperlink
                (this.grdTenant, "Select$" + e.Row.RowIndex);
            e.Row.Attributes.Add("onclick", String.Format("javascript:$find('{0}').show();",ModalEditTenant.ClientID ));

//this line does not work in retrieving row data
   IDataRecord dataRow = (IDataRecord)e.Row.DataItem;
        txtRPCode.Text = Convert.ToString(dataRow["Retail Partner"]);

        }
    }

作为替代方案,我这样做是为了实现第二个目标,但未能实现第一个目标

  protected void grdTenant_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridViewRow row = grdTenant.SelectedRow;
        txtRPCode.Text  = row.Cells[1].Text;

        ModalEditTenant.Show();
    }

But, this method works only when the select button column is visible and when it is clicked, it fails to do so when the full row is selected.

如何使整行选择并成功将数据检索到文本框(隐藏选择列)


这就是gridview从存储过程数据源绑定数据的方式。

     System.Threading.Thread.Sleep(500);
            //DropDownList ddl = (DropDownList)sender;

            SqlCommand cmd = new SqlCommand("spTenantList", con);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@Location", ddlSelectLoc.SelectedValue );


            try
            {
                con.Open();
                grdTenant.EmptyDataText = "No Records Found";
                grdTenant.DataSource = cmd.ExecuteReader();
                grdTenant.DataBind();


            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                con.Close();
                con.Dispose();
            }

【问题讨论】:

  • 能否在您的问题中添加“gridviewid.datasource”绑定代码??
  • 我已经更新了我的问题
  • 不是 .aspx 文件代码,我需要将数据绑定到 gridview 的 C# 代码。前任。 grdTenant.DataSource = XYZ; grdTenant.DataBinding();...等您是否使用数据表来绑定您的网格?
  • 我又更新了,之前说过我是用存储过程来加载gridview中的一些数据,在我的下拉列表中,当用户选择一个item时,gridview会自动加载数据基于选择
  • 不要使用try {} catch(Exception ex){throw ex;}。它所做的只是弄乱堆栈跟踪,使其看起来异常来自throw

标签: c# sql asp.net gridview


【解决方案1】:

使用FindControl()方法。

TextBox ID代替txtRetailPartner

txtRPCode.Text = ((TextBox)e.Row.FindControl("txtRetailPartner")).Text;

【讨论】:

  • 我有这个错误,在这样做之后 - 对象引用未设置为对象的实例。
  • 您的文本框名称是什么?您是否在上面的行中替换了它并尝试?
  • 是的,我将 txtRetailPartner 替换为实际文本框的名称,即 txtRPCode。
  • txtRPCode 在gridview里面?您试图从 gridview 到 textbox 获得哪个值?
  • 不,在 gridview 先生中没有定义边界字段或模板字段。
【解决方案2】:

RowDataBound事件中使用我下面的逻辑来获取DataItem

protected void grdTenant_RowDataBound(object sender, GridViewRowEventArgs e)
{
    e.Row.Cells[0].Style["display"] = "none";

    if (e.Row.RowType == DataControlRowType.DataRow)
    { 
         IDataRecord dataRow = (IDataRecord)e.Row.DataItem;
         txtRPCode.Text = Convert.ToString(dataRow["Retail Partner Code"]);
    }
}

【讨论】:

  • 我在代码中的 onclick 部分之后包含您的代码,它给了我这个错误:错误 3 'System.Web.UI.WebControls.GridViewRowEventArgs' 不包含 'Item' 的定义和找不到接受“System.Web.UI.WebControls.GridViewRowEventArgs”类型的第一个参数的扩展方法“Item”(您是否缺少 using 指令或程序集引用?)
  • 是的,它在弹出窗口中
  • 要在txtRPCode文本框中显示哪个GridView列数据?你能给我列的索引吗,比如第一列,第二列,……等等???
  • 实际上gridview中的所有列的任务是移动或显示在模式弹出窗口中,因为它们用于更新/编辑功能,只是为了使问题更简单,我给出了第一列在 gridview 中,它是零售合作伙伴代码(根据存储过程的列名称),并且要在模式弹出扩展器的 txtRPCode 文本框中传递。在当前情况下,项目未被接受我是否缺少程序集或命名空间?
  • txtRPCode 是第一列具体回答您的问题。
【解决方案3】:

我已经根据要实现的目标找到了答案。使用我当前作为问题发布的代码,只需删除那里的一行代码,它就会起作用

protected void grdTenant_RowDataBound(object sender, GridViewRowEventArgs e) { e.Row.Cells[0].Style["display"] = "none";

    if (e.Row.RowType == DataControlRowType.DataRow)
    { 

        e.Row.Attributes["onmouseover"] =
            "javascript:setMouseOverColor(this);";
        e.Row.Attributes["onmouseout"] =
            "javascript:setMouseOutColor(this);";
        e.Row.Attributes["onclick"] =
        Page.ClientScript.GetPostBackClientHyperlink
            (this.grdTenant, "Select$" + e.Row.RowIndex);        
    }
}

此行已删除:

//这将不再需要,因为它将使onselectedindexchanged事件无效,因为它将首先运行。

e.Row.Attributes.Add("onclick", String.Format("javascript:$find('{0}').show();",ModalEditTenant.ClientID ));

protected void grdTenant_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridViewRow row = grdTenant.SelectedRow;
        txtRPCode.Text  = row.Cells[1].Text;

        ModalEditTenant.Show();
    }

一切都会按预期进行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-09
    • 1970-01-01
    • 2014-06-26
    • 1970-01-01
    • 2014-08-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多