点击GridView中查找所选行 [英] Click GridView Find Selected Row

查看:161
本文介绍了点击GridView中查找所选行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图采取GridView和取回从被点击该行的数据。我试着下面的代码,当我点击该行,我回去将所选的索引,但是当我看到在GridView的实际行他们显示为空。不知道我错过。



.ASP让我的网格。

 < ASP:GridView控件ID =GridView1=服务器的AutoGenerateColumns =真
的CssClass =数据表WIDTH =100%
的DataSourceID =SqlDataSource1
网格线=无ShowFooter =真AllowSorting =真
onrowcreated =GridView1_RowCreated
onrowdatabound =GridView1_RowDataBoundShowHeaderWhenEmpty =真
onrowcommand =GridView1_RowCommand
onselectedindexchanged =GridView1_SelectedIndexChanged>
< HeaderStyle的CssClass =hdrow/>
< RowStyle的CssClass =数据行/>
< PagerStyle的CssClass =cssPager/>
< / ASP:GridView的>

在绑定的每一行数据我确保点击应该设置选定的索引。



 保护无效GridView1_RowDataBound(对象发件人,GridViewRowEventArgs E)
{
如果(e.Row.RowType == DataControlRowType。 DataRow中)
{
e.Row.Attributes [的onclick] = Page.ClientScript.GetPostBackClientHyperlink(GridView1,选择$+ e.Row.RowIndex);
}
}



然后,当点击这个选择的指数的变化,都会激发我可以把一个断点在第一行,我看到的是我点击获取存储在索引中。然而,当我到达的foreach它跳过过去的权利,因为它显示为0行的计数GridView1。从理论上讲它应该有几百行和索引相匹配时,它应该在6单元上获取数据,并存储在字符串湾为什么我的点击没有得到行?



 保护无效GridView1_SelectedIndexChanged(对象发件人,EventArgs五)
{
int类型的= GridView1.SelectedIndex

的foreach(GridViewRow行GridView1.Rows)
{
如果(row.RowIndex ==一)
$ { b $ = BB row.Cells [6]。文本;
}
}
}

下面是我的页面加载。

 保护无效的Page_Load(对象发件人,EventArgs五)
{
C = HttpContext.Current.Session [ C]的ToString()。
SqlDataSource1.ConnectionString = //我的秘密
串strSelect =选择表名COLUMNNAMES其中c在(@c)
SqlDataSource1.SelectParameters.Clear();
SqlDataSource1.SelectCommand = strSelect;
SqlDataSource1.SelectParameters.Add(C,C);

{
GridView1.DataBind();
}
抓(例外五)
{

}
GridView1.AutoGenerateColumns = TRUE;
}


解决方案

尝试刚刚从SelectedRow抢夺财产的行:

 保护无效GridView1_SelectedIndexChanged(对象发件人,EventArgs五)
{
GridViewRow排= GridView1.SelectedRow;
字符串b = row.Cells [6]。文本;
}

这是我的理解是,行集合不会得到回发,当你重新填充使用这些数据源控件(如SqlDataSource的)。



您很可能使用现有的代码,如果你叫 .DataBind()在你的GridView试图重复之前通过行:

  GridView1.DataSourceID =SqlDataSource1; 
GridView1.DataBind();



但是,这似乎有点哈克。






看到你的Page_Load后,我看你需要用你绑定的代码在如果(!Page.IsPostBack)块。在每次回传的数据绑定在中断的ASP.NET通过ViewState的维护您的控件的状态的过程。


I'm trying to take a GridView and get back the data from the row that was clicked. I've tried the code below and when I click the row I get back the selected index but when I look at the actual rows in the GridView they show empty. Not sure what I am missing.

.ASP make my grid.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="True" 
        CssClass="datatables" Width="100%" 
        DataSourceID="SqlDataSource1" 
        GridLines="None" ShowFooter="True" AllowSorting="True"  
        onrowcreated="GridView1_RowCreated" 
        onrowdatabound="GridView1_RowDataBound" ShowHeaderWhenEmpty="True" 
        onrowcommand="GridView1_RowCommand" 
        onselectedindexchanged="GridView1_SelectedIndexChanged">
        <HeaderStyle CssClass="hdrow" />
        <RowStyle CssClass="datarow" />
        <PagerStyle CssClass="cssPager" />
</asp:GridView>

On each row data bound I make sure that the click should set the selected index.

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
     if (e.Row.RowType == DataControlRowType.DataRow)
     {
        e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(GridView1, "Select$" + e.Row.RowIndex);
     }
 }

Then when the selected index changes by clicking this gets fired which I can put a breakpoint on the first line and I see the index of what I clicked on get stored in a. However when I get to the foreach it skips right past it because it shows GridView1 having a Count of 0 rows. In theory it should have a couple hundred rows and when the index matches it should grab the data in the 6th cell over and store it in string b. Why am I getting no rows on the click?

 protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
 {
            int a = GridView1.SelectedIndex

            foreach (GridViewRow row in GridView1.Rows)
            {
                if (row.RowIndex == a)
                {
                    b = row.Cells[6].Text;
                }
            }
 }

Here is my page load.

protected void Page_Load(object sender, EventArgs e)
{
      c = HttpContext.Current.Session["c"].ToString();
      SqlDataSource1.ConnectionString = //My secret
      string strSelect = "SELECT columnnames from tablenames where c in (@c)
      SqlDataSource1.SelectParameters.Clear();
      SqlDataSource1.SelectCommand = strSelect;
      SqlDataSource1.SelectParameters.Add("c", c);
       try
        {
            GridView1.DataBind();
        }
        catch (Exception e)
        {

        }
        GridView1.AutoGenerateColumns = true;
}

解决方案

Try just grabbing the row from the SelectedRow property:

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
    GridViewRow row = GridView1.SelectedRow;
    string b = row.Cells[6].Text;
}

It's my understanding that the Rows collection doesn't get repopulated on PostBacks when you're using those data source controls (like SqlDataSource).

You could probably use your existing code if you called .DataBind() on your GridView prior to trying to iterate through the Rows:

GridView1.DataSourceID="SqlDataSource1";
GridView1.DataBind();

But that seems a little hacky.


After seeing your Page_Load, I see that you need to wrap your databinding code in an if(!Page.IsPostBack) block. Databinding on every postback is interrupting the process of ASP.NET maintaining the state of your controls via the ViewState.

这篇关于点击GridView中查找所选行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆