我有2个sql来选择table.program运行网格视图show number 1 sql。
protected void Page_Load(object sender, EventArgs e)
{
btn_submit.Visible = false;
SqlConnection conn = new SqlConnection(@"Data Source=HENDRICK-PC\SQLEXPRESS;Initial Catalog=mylibrary.com;Integrated Security=True");
conn.Open();
string sql = "select * from PEMINJAMAN";
SqlCommand com = new SqlCommand(sql, conn);
GridView1.DataSource = com.ExecuteReader();
GridView1.DataBind();
conn.Close();
}
但我可以将sql1更改为sql2,只需单击按钮即可。
protected void btn_search_Click(object sender, EventArgs e)
{
try
{
close();
GridView1.DataSource = null;
GridView1.DataBind();
lbl_error.Visible = false;
SqlConnection conn = new SqlConnection(@"Data Source=HENDRICK-PC\SQLEXPRESS;Initial Catalog=mylibrary.com;Integrated Security=True");
conn.Open();
string sql = "select * from PEMINJAMAN where NAMA_PEMINJAM like '" + txt_search.Text + "%' or ALAMAT_PEMINJAM like '" + txt_search.Text + "%' or HP_PEMINJAM like '" + txt_search.Text + "%' or KODE_BUKU like '" + txt_search.Text + "%' or TGL_PINJAM like '" + txt_search.Text + "%' or TGL_KEMBALI like '" + txt_search.Text + "%'";
SqlCommand com = new SqlCommand(sql, conn);
GridView1.DataSource = com.ExecuteReader();
GridView1.DataBind();
conn.Close();
}
catch
{
lbl_error.Visible = true;
}
}
我必须从gridviewselectrow获取数据。但问题是事件得到了错误的row.i将gridview更改为sql2并且我选择行但gridview将自动更改为sql1并从sql1.not sql2.it中获取我的gridview事件
protected void GridView1_SelectedIndexChanging1(object sender, GridViewSelectEventArgs e)
{
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,-";
}
}
和我的gridview。
<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>
如何解决?对不起我的英语太糟糕了。 THX
答案 0 :(得分:0)
SelectedIndexChanging
事件会返回您选择的新行,而不是当前选定的行,您可以在此处取消选择。要获取当前选定的行,您必须使用 SelectedIndexChanged
事件并通过 SelectedRow
访问它。
protected void GridView1_SelectedIndexChanged(Object sender, EventArgs e)
{
btn_submit.Visible = true;
lbl_nama.Text = GridView1.SelectedRow.Cells[1].Text;
....
}