【发布时间】:2015-04-16 09:49:30
【问题描述】:
我有一个从存储过程加载数据的gridview。
目标:
通过在 gridview 中隐藏选择列来使整个 gridview 行可点击 - 我可以通过在 RowDataBound 事件中执行以下代码来做到这一点。
接下来,当我单击网格视图中的整行时,我应该能够从所选行中获取数据/值,并将其显示在弹出模式的文本框中。这是用于更新/编辑功能。
这就是我得到的东西
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。