如何在gridview中单击编辑按钮时选择行

本文关键字:按钮 编辑按 选择 编辑 单击 gridview | 更新日期: 2023-09-27 18:20:14

我已经创建了带有编辑、更新和删除功能的gridview。

当我选择链接时,将选择特定的行。但当我单击编辑按钮时,所选的行并没有被选中。

我可以知道,我该怎么解决这个问题吗?

这是我的cs文件:

protected void btnsub_Click(object sender, EventArgs e)
        {
            SqlConnection con = Connection.DBconnection();
            if (Textid.Text.Trim().Length > 0)
            {
                SqlCommand com = new SqlCommand("StoredProcedure3", con);
                com.CommandType = CommandType.StoredProcedure;
                com.Parameters.AddWithValue("@id", Textid.Text.Trim());
                com.Parameters.AddWithValue("@Name", Textusername.Text.Trim());
                com.Parameters.AddWithValue("@Class", Textclass.Text.Trim());
                com.Parameters.AddWithValue("@Section", Textsection.Text.Trim());
                com.Parameters.AddWithValue("@address", Textaddress.Text.Trim());
                com.ExecuteNonQuery();
                GridViewRow gr = GridView1.SelectedRow;
                gr.Cells[1].Text = Textid.Text;
                gr.Cells[2].Text = Textusername.Text;
                gr.Cells[3].Text = Textclass.Text;
                gr.Cells[4].Text = Textsection.Text;
                gr.Cells[5].Text = Textaddress.Text;

            }
            else
            {
                SqlCommand com = new SqlCommand("StoredProcedure1", con);
                com.CommandType = CommandType.StoredProcedure;
                com.Parameters.AddWithValue("@Name", Textusername.Text.Trim());
                com.Parameters.AddWithValue("@Class", Textclass.Text.Trim());
                com.Parameters.AddWithValue("@Section", Textsection.Text.Trim());
                com.Parameters.AddWithValue("@address", Textaddress.Text.Trim());
                com.ExecuteNonQuery();
                Response.Redirect("studententry.aspx");
            }
        }
        protected void btnrst_Click(object sender, EventArgs e)
        {
            Textusername.Text = null;
            Textclass.Text = null;
            Textsection.Text = null;
            Textaddress.Text = null;
        }
        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            SqlConnection con = Connection.DBconnection();
            if (e.CommandName == "EditRow")
            {
                GridViewRow gr = (GridViewRow)((Button)e.CommandSource).NamingContainer;               
                Textid.Text = gr.Cells[1].Text;
                Textusername.Text = gr.Cells[2].Text;
                Textclass.Text = gr.Cells[3].Text;
                Textsection.Text = gr.Cells[4].Text;
                Textaddress.Text = gr.Cells[5].Text;
            }
            else if (e.CommandName == "Deleterow")
            {
                GridViewRow gr = (GridViewRow)((Button)e.CommandSource).NamingContainer;      
                SqlCommand com = new SqlCommand("StoredProcedure4", con);
                com.CommandType = CommandType.StoredProcedure;
                com.Parameters.AddWithValue("@ID", gr.Cells[0].Text);
                var id = Int32.Parse(e.CommandArgument.ToString());                
                GridView1.Rows[id].Visible = false;
                com.ExecuteNonQuery();
            }
        }

这是我的aspx:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataKeyNames="ID" DataSourceID="SqlDataSource1" 
        OnRowCommand="GridView1_RowCommand" AutoGenerateSelectButton="True" 
        EnablePersistedSelection="True" BackColor="White">
        <Columns>
            <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" 
                ReadOnly="True" SortExpression="ID" />
            <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
            <asp:BoundField DataField="Class" HeaderText="Class" SortExpression="Class" />
            <asp:BoundField DataField="Section" HeaderText="Section" 
                SortExpression="Section" />
            <asp:BoundField DataField="Address" HeaderText="Address" 
                SortExpression="Address" />
            <asp:TemplateField HeaderText="Edit">
               <ItemTemplate>
                 <asp:Button runat="server" ID="btnedit" Text="Edit" CommandName="EditRow"></asp:Button>                    
               </ItemTemplate>
                <ControlStyle BorderColor="#CCFF66" />
          </asp:TemplateField>
          <asp:TemplateField HeaderText="Delete">
          <ItemTemplate>
                 <asp:Button runat="server" ID="btndelete" Text="Delete" CommandArgument='<%# Container.DataItemIndex %>' CommandName="Deleterow"></asp:Button>                    
               </ItemTemplate>
          </asp:TemplateField>            
        </Columns>
        <SelectedRowStyle BackColor="#FF66FF" />
    </asp:GridView>

我检查了设计,我确信enableselectedrowbutton是真的。当我点击编辑按钮时,在放置断点后,它显示空值,这意味着所选的行不起作用。这是我的输出屏幕截图

有人能帮我解决这个问题吗?

谢谢,

如何在gridview中单击编辑按钮时选择行

您可以按照以下三种方法在单击按钮时高亮显示当前行。

方法1:

在gridview标记和代码隐藏中添加OnSelectedIndexChanged事件。

GridView控件标记

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataKeyNames="ID" DataSourceID="SqlDataSource1" 
        OnRowCommand="GridView1_RowCommand" AutoGenerateSelectButton="True" 
        EnablePersistedSelection="True" BackColor="White"
        OnSelectedIndexChanged = "OnSelectedIndexChanged"
        EnableViewState="true">
</asp:GridView>

代码隐藏

protected void OnSelectedIndexChanged(object sender, EventArgs e)
{
    foreach (GridViewRow row in GridView1.Rows)
    {
        if (row.RowIndex == GridView1.SelectedIndex)
        {
            row.BackColor = ColorTranslator.FromHtml("#A1DCF2");
        }
        else
        {
            row.BackColor = ColorTranslator.FromHtml("#FFFFFF");
        }
    }
}

但在这里,您将在gridview中循环,直到获得选定的行索引。这将降低性能。

方法2:

下面的方法比前面的要好得多。在这里,您不需要循环,只需提及以下按钮下的点击事件休息将全部完成。我还没有在IDE中测试过这个,也不能保证它成功。

Button btnEdit = Button as sender;
GridViewRow gvr = (GridViewRow)btnEdit.NamingContainer;
int rowindex = gvr.RowIndex;
if (rowindex == GridView1.SelectedIndex)
{
    gvr.BackColor = ColorTranslator.FromHtml("#A1DCF2");
}
else
{
    gvr.BackColor = ColorTranslator.FromHtml("#FFFFFF");
}

方法3:

由于您正在使用RowCommand,您可以应用以下内容:-

int rowindex = Convert.ToInt32(e.CommandArgument);
GridViewRow gvr = GridView1.Rows(rowindex);
if (rowindex == GridView1.SelectedIndex)
{
    gvr.BackColor = ColorTranslator.FromHtml("#A1DCF2");
}
else
{
    gvr.BackColor = ColorTranslator.FromHtml("#FFFFFF");
}