【问题标题】:How to get a selected column's data in a string from the gridview如何从gridview的字符串中获取选定列的数据
【发布时间】:2013-10-26 11:25:27
【问题描述】:

我在页面加载事件的 Gridview 控件中绑定数据。

如何从 Gridview 控件中获取字符串中选定列的数据?

(即)它是该表的主键,因此对于用户选择的每一行,都会有一个与其绑定的主键 ID 列。因此,我需要获取该 ID,并且使用此 ID,我需要链接到我的 Web 应用程序的下一页。

使用 Visual Studio 2005 asp.net C# 2.0。

以下是我尝试过的一些代码:

protected void SubmitButton_Click(object sender, EventArgs e)
{
string bS = Convert.ToString(gridview1.Rows[rowIndex].Cells[2].Text);
}

【问题讨论】:

  • 使用您的 gridView 的 SelectedRow 属性。以下链接有一些方法可以通过更好的解释来完成您的工作。 msdn.microsoft.com/en-us/library/…
  • 发布您的 gridview 代码!您可以使用许多选项来表示 Selected Row 属性、RowCommand 等。

标签: c# asp.net .net gridview binding


【解决方案1】:

做这样的事情:

编辑

向您的标记添加隐藏字段:

<asp:HiddenField ID="hdnID" runat="server" />
<asp:GridView ID="GridView1" runat="server" 
OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
... ... ...   ... ... ...   ... ... ...   ... ... ...

在 GridVew 的 selectedIndexChanged 事件中,将值设置为隐藏字段:

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
    GridViewRow row = GridView1.SelectedRow;
    hdnID.Value = row.Cells[2].Text;
}

在按钮事件中从隐藏字段中获取值:

protected void SubmitButton_Click(object sender, EventArgs e)
{
    string bS = hdnID.Value;
}

【讨论】:

  • 此代码获取所需列的每个值,而不是仅在 gridview 中获取所选行的一个值
  • 是的,这就是我通常所做的。我什至有一些复选框,并在网格中循环,只抓取复选框中的名称。
【解决方案2】:

您可以使用RowCommand 事件。以下是我的代码示例。您应该使用DataKeys 在GridView 中提供一个按钮字段并提供CommandName = Select

   protected void GridView_AccGroups_RowCommand(object sender, GridViewCommandEventArgs e)
   {
       if (e.CommandName == "Select")   //commandName given to the button field in the Gridview
       {

            foreach (GridViewRow i in GridView_AccGroups.Rows)   //takes each Gridview Row..my Gridview name is 'GridView_AccGroups'
            {
                try
                {
                    int index = Convert.ToInt32(e.CommandArgument);    // gets the selected Index
                    GridViewRow selectedRow = GridView_AccGroups.Rows[index];  // with the selected index gets the selected Row 
                    string txtAccGroupName = GridView_AccGroups.DataKeys[index].Values["Account Group"].ToString();  // DataKey Names are given in the Gridview Properties..!! each key name in one line,these key names are column names in the result of the select query that is bound to the Gridview,
                    string txtAccGroupShortName = GridView_AccGroups.DataKeys[index].Values["Short Name"].ToString();

                    string  txtAccGroupRemarks = GridView_AccGroups.DataKeys[index].Values["Remarks"].ToString();
                    string id = GridView_AccGroups.DataKeys[index].Values["ID"].ToString();

                    string = GridView_AccGroups.DataKeys[index].Values["Parent Group ID"].ToString();
                }
                catch (Exception ex)
                {

                }
            }
        }          
    }

【讨论】:

    猜你喜欢
    • 2012-10-02
    • 1970-01-01
    • 1970-01-01
    • 2015-07-11
    • 2010-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多