webForumDet fria alternativet

Ta bort rad ur gridview

.NETur .NET

4 svar · 556 visningar · startad av invecklaren

Medlem sedan jan. 2008280 inlägg
Frågan#1

Hej!

Jag har en gridview där jag skulle vilja ha möjligheten att ta bort en rad utan att binda om gridviewen. Är det möjligt?

Tanken är att detta skulle ske i gridviewens rowcommand event.

Medlem sedan sep. 20011 914 inlägg
#2
<asp:gridview id="GridView1" runat="server" allowpaging="True" autogeneratecolumns="False" onrowcreated="GridView1_RowCreated" onrowcommand="GridView1_RowCommand">
            <columns>
            <asp:templatefield>
                        <itemtemplate>
                                    <asp:button id="bt1" text="Remove" commandname="remove" runat="server" />
                        </itemtemplate>
            </asp:templatefield>
            </columns>
</asp:gridview>
        protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                List<int> col = ViewState["grr"] == null ? new List<int>() : ViewState["grr"] as List<int>;

                int index = (GridView1.PageIndex * GridView1.PageSize) + e.Row.RowIndex;
                ((Button)e.Row.FindControl("bt1")).CommandArgument = index.ToString();
                if (col.Contains(index))
                    e.Row.Visible = false;
            }
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "remove")
            {
                Button bt = e.CommandSource as Button;
                int index1 = int.Parse(bt.CommandArgument);
                int index2 = index1 - (GridView1.PageIndex * GridView1.PageSize);
                GridView1.Rows[index2].Visible = false;

                List<int> col = ViewState["grr"] == null ? new List<int>() : ViewState["grr"] as List<int>;
                if (!col.Contains(index1))
                    col.Add(index1);

                ViewState["grr"] = col;
            }
        }
Medlem sedan jan. 2008280 inlägg
#3

Jag beundrar din kunskap. Du är ett äkta proffs.

Medlem sedan sep. 2004828 inlägg
#4

Spännande att du sätter ditt eget inlägg som lösningen.

Medlem sedan jan. 2008280 inlägg
#5

Haha, till skillnad från Dino är jag inte ofelbar. Nu är det fixat.

123 ms totalt · 3 externa anrop · v20260731065814-full.4bcf49fe
0 ms — hämta forumlista (cache)
0 ms — hämta statistik (cache)
120 ms — hämta tråd, inlägg och bilagor (db)