更改表gridview选择错误的行

时间:2015-10-15 18:12:03

标签: c# asp.net gridview

我有页面加载从数据库加载gridview 我有文本框和搜索按钮可以更改gridview。但问题是gridview_selectindexchanging选择错误的行。

        protected void GridView1_SelectedIndexChanging1(object sender, GridViewSelectEventArgs e)
    {

        lbl_1.Visible = true;
        lbl_2.Visible = true;
        lbl_3.Visible = true;
        lbl_4.Visible = true;
        lbl_5.Visible = true;
        lbl_6.Visible = true;
        lbl_7.Visible = true;
        btn_submit.Visible = true;
        lbl_nama.Text = GridView1.Rows[e.NewSelectedIndex].Cells[1].Text;
        lbl_alamat.Text = GridView1.Rows[e.NewSelectedIndex].Cells[2].Text;
        lbl_hp.Text = GridView1.Rows[e.NewSelectedIndex].Cells[3].Text;
        lbl_kode.Text = GridView1.Rows[e.NewSelectedIndex].Cells[4].Text;
        lbl_peminjaman.Text = GridView1.Rows[e.NewSelectedIndex].Cells[5].Text;
        lbl_pengembalian.Text = GridView1.Rows[e.NewSelectedIndex].Cells[6].Text;

        DateTime kembali = DateTime.ParseExact(lbl_pengembalian.Text,"dd-MM-yyyy",CultureInfo.InvariantCulture);
        if (DateTime.Now > kembali)
        {
            lbl_denda.Text = "Rp.20000,-";
        }
        else
        {
            lbl_denda.Text = "Rp.0,-";
        }
    }

            <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" style="text-align: left" AutoGenerateSelectButton="true" OnSelectedIndexChanging="GridView1_SelectedIndexChanging1">
            <AlternatingRowStyle BackColor="White" />
            <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
            <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
            <SortedAscendingCellStyle BackColor="#FDF5AC" />
            <SortedAscendingHeaderStyle BackColor="#4D0000" />
            <SortedDescendingCellStyle BackColor="#FCF6C0" />
            <SortedDescendingHeaderStyle BackColor="#820000" />
        </asp:GridView>

如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

我认为您可能正在寻找EnablePersistedSelection属性。

  

获取或设置一个值,该值指示是否选择行   基于索引或数据键值

     

如果此属性为false且选择了行,则为同一行   即使新页面具有,也会在显示新页面时选择   不同的数据。如果您将此属性设置为true,那么当您   显示其中包含不同数据的页面,不选择任何行。如果   然后返回到选择了行的页面,该行是   仍然被选中。