简体   繁体   English

如何在asp .net和c#中的gridview中启用选定的行

[英]how to enable selected row in gridview in asp .net and c#

Here is my aspx:这是我的aspx:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataKeyNames="ID" DataSourceID="SqlDataSource1" 
        OnRowCommand="GridView1_RowCommand" AutoGenerateSelectButton="True" EnablePersistedSelection="True">
        <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>
          </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>
    </asp:GridView>

here is code behind:这是后面的代码:

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 = Textusername.Text;
                gr.Cells[2].Text = Textclass.Text;
                gr.Cells[3].Text = Textsection.Text;
                gr.Cells[4].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");
            }
        }

The selected row did not select in gridview.所选行未在 gridview 中选择。 May i know how to enable gridview row.我可以知道如何启用 gridview 行。 I used msdn and other documents and i followed,but nothing helps.我使用了 msdn 和其他文件,我也跟着做了,但没有任何帮助。

I design i set enable selection, but still i didn't find out issue.我设计我设置了启用选择,但我仍然没有发现问题。

Can anyone help me?谁能帮我?

Thanks,谢谢,

Basically we get SelectedRow of GridView Upon some GridviewRow Action event like OnSelectedIndexChanged , OnSelectedIndexChanging , OnRowEditing or from Template control like Button click.基本上,我们在一些 GridviewRow 操作事件(如OnSelectedIndexChangedOnSelectedIndexChangingOnRowEditing或来自模板控件(如Button单击))上获得 GridView 的 SelectedRow。 But here in your coding I don't think your btnsub_Click is inside Gridview so If you want to use the SelectedGridViewRow after your GridviewRow Action event then save the index in some temporary variable or pass the variable to method as argument.但是在您的编码中,我认为您的btnsub_Click Gridview 内,因此如果您想在 GridviewRow Action 事件之后使用 SelectedGridViewRow,则将索引保存在某个临时变量中或将该变量作为参数传递给方法。

protected void GridView1_SelectedIndexChanged(Object sender, EventArgs e)
  {
    int index = Gridview1.SelectedIndex;
    hiddenfield.Value = index.ToString();
  }

OR In OnRowEditing also you can get the index of row在 OnRowEditing 中,您也可以获得行的索引

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "EditRow")
            {
                GridViewRow gr = (GridViewRow)((Button)e.CommandSource).NamingContainer;
                int index = gr.RowIndex;
                hidval.Value = index.ToString();
            }
        }

and on button Click you can get the hiddenfield value as index:并在按钮上单击您可以获得隐藏字段值作为索引:

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();
                if(!String.IsNullOrEmpty(hiddenfield.Value))
                {
                int index = Convert.ToInt16(hiddenfield.Value);
                GridView1.Rows[index].Cells[1].Text = Textusername.Text;
                GridView1.Rows[index].Cells[2].Text = Textclass.Text;
                GridView1.Rows[index].Cells[3].Text = Textsection.Text;
                GridView1.Rows[index].Cells[4].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");
            }
        } 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM