简体   繁体   English

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

[英]how to select row when an edit button in gridview

I have been created gridview with edit,update and delete.我已经创建了具有编辑、更新和删除功能的 gridview。

when i select link, particular row will be selected.当我选择链接时,将选择特定行。 but when i click edit button, the selected rows didn't select.但是当我点击编辑按钮时,选定的行没有选择。

May i know, how can i fix this?我可以知道,我该如何解决这个问题?

Here is my cs file:这是我的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();

            }
        }

and here is my aspx:这是我的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>

and i checked in design and I'm sure that enable selectedrowbutton is true.我检查了设计,我确定启用 selectedrowbutton 是真的。 When i click edit button, after put breakpoint it shows null value, means, the selectedrow didn't work.当我单击编辑按钮时,在放置断点后它显示空值,这意味着所选行不起作用。 Here is my output screenshot这是我的输出截图

Can anyone help me to fix this?谁能帮我解决这个问题?

Thanks,谢谢,

You can follow following three approach to highlight current row on button click.您可以按照以下三种方法在按钮单击时突出显示当前行。

Approach 1:方法一:

Adding OnSelectedIndexChanged event in gridview markup and code-behind.在 gridview 标记和代码隐藏中添加OnSelectedIndexChanged事件。

GridView Control Mark-up 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>

Code-Behind代码隐藏

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");
        }
    }
}

But here you will be looping inside gridview till you get the selected row index.但在这里,您将在 gridview 中循环,直到获得选定的行索引。 This will downgrade the performance.这会降低性能。

Approach 2:方法二:

Following approach is far more better than previous one.下面的方法比以前的方法要好得多。 Here, you won't be needing to loop just mention following under button click event rest all will be done.在这里,您将不需要循环,只需在按钮单击事件下提及以下内容即可完成。 I haven't tested this one in IDE and can't guarantee it success.我没有在 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");
}

Approach 3:方法三:

Since you are using RowCommand you can apply following:-由于您使用的是 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");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果标题没有数据绑定,单击“编辑”按钮时,如何在GridView行中获取TextBox? - How do I get TextBox in a GridView row when Edit button is clicked if the headers are not databound? 单击按钮后如何在GridView中编辑一个选定的行? - How do I edit one selected row in GridView on a button click? 如何在没有选择按钮的情况下选择和取消选择GridView中的行? - How to select and unselect the row in the GridView without having select button? 隐藏gridview中最后一行的编辑和删除按钮 - Hide edit & delete button for last row in gridview 在gridview中单击“编辑”按钮时,如何调用“删除行”事件? asp.net C# - How can I call “delete row ” event when i click on “edit” button in gridview? asp.net C# 如何在没有 select 按钮的情况下在 GridView 中实现全行选择? - How to implement full row selecting in GridView without select button? 连续按下按钮时如何获取gridview中的一行ID - How to take ID of a row in gridview when you press a button in a row 有什么方法可以使用自定义编辑按钮在gridview中编辑绑定字段的行? - Is there any way to Edit row of bound fields in gridview with custom edit button? 单击“编辑 - 按钮”时,GridView消失 - GridView dissapears when clicking on Edit-Button 同一gridview行上的编辑按钮和下一页链接按钮 - Edit button and next page link button on same gridview row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM