[英]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.