How to create a dynamic Gridview?

Asked

Viewed 2,484 times

2

Someone already created a GridView where there are already rows filled with values and in the last column of each row there is a LinkButton "Repeat" and when this LinkButton is clicked, repeats the line just below the clicked line?

I’m using C#.

  • When an answer solves your problem, it is advisable to accept it. Just click on the "check" sign below the votes of the question. It will turn green. You and the author of the answer gain reputation for this.

1 answer

1


That’s perfectly possible, but it’s not the easiest thing to do. In fact, anything to do in Web Forms that is not directly provided by the platform has its complications. As such, this can be confusing at first. I will try to explain step by step.

First, in your GridView you must have a TemplateField to place the "Repeat" button on ItemTemplate.

<asp:TemplateField>
    <ItemTemplate>
        <asp:LinkButton runat="server" Text="Repetir" CommandName="Repetir"
            CommandArgument="<%#Container.DataItemIndex %>" />
    </ItemTemplate
</asp:TemplateField>

Now you need to listen to your Gridview’s Rowcommand event. Just add something like OnRowCommand="MinhaGridView_RowCommand" in Gridview and create method in Codebehind:

protected void MinhaGridView_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Repetir") MinhaGridViewRepetirLinha(e.CommandArgument);
}

Now to the Minhagridviewrepetinha method:

protected void MinhaGridViewRepetirLinha(string sLinha)
{
    var numeroDaLinha = int.Parse(sLinha);
    DataSourceDaGridView.Insert(numeroDaLinha, DataSourceDaGridView[numeroDaLinha]);
    MinhaGridView.DataSource = DataSourceDaGridView;
    MinhaGridView.DataBind();
}

Note that for this I used a List<> as DataSource of GridView. I kept her using ViewState. I’ll show you more or less how to implement.

private List<object> _dataSourceDaGridView;
protected List<object> DataSourceDaGridView
{
    get
    {
        return _dataSourceDaGridView ?? (_dataSourceDaGridView = ViewState["Linhas"] as List<object> ?? new List<object>());
    }
    set
    {
        _dataSourceDaGridView = ViewState["Linhas"] = value;
    }
}

I didn’t test that code, I just based it on a code I’ve already used. DO NOT COPY AND PASTE, OBSERVE THE CODE AND ADAPT IT TO YOUR NEEDS, AND CORRECT POSSIBLE ERRORS ALONG THE WAY.

By the way, if any mistakes are glaring, please let me know to edit my answer.

  • gave an error: (The index was outside the range. It must be non-negative and smaller than the collection size. Parameter name: index) The error is occurring inside the 'Minhagridviewrepetirlinha' method in the 'Datasourcedagridview.Insert(numeroDaLinha, Datasourcedagridview[numeroDaLinha])'. Before I should not fill 'Datasourcedagridview' with the query I used to fill my 'Gridview', so that he can know below which line he will insert the new line?

  • Yes, fill in your Datasource before using it.

  • It worked, thank you very much!

  • @Rafaelamorim Don’t forget to accept the answer if it has been useful to you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.