How to fill gridview asp.net c # sql from behindcode? [closed]

0

I have a webform application in ASP .NET with C # and I want to fill a gridview from the webform.aspx.cs. There is some way to fill it "only" when entering the page, or it must be only with a click event. Could you tell me how to do it? I will greatly appreciate your help Thank you Greetings

    
asked by sunflower 07.03.2018 в 19:37
source

2 answers

0

This is my Table or Grid in aspx (delete some columns because I use a lot and they are from my work ..)

 <asp:GridView ID="dtg_CutWeek" runat="server" AutoGenerateColumns="False" OnRowCommand="dtg_CutWeek_RowCommand">
                    <Columns>
                        <asp:BoundField DataField="Year" HeaderText="AÑO">
                            <HeaderStyle HorizontalAlign="Center" />
                            <ItemStyle Width="50px" HorizontalAlign="Center" />
                        </asp:BoundField>

                        <asp:BoundField HeaderText="MONEDA" DataField="TC">
                            <HeaderStyle HorizontalAlign="Center" />
                            <ItemStyle Width="60px" HorizontalAlign="Center" />
                        </asp:BoundField>
                    </Columns>

and to do the bindeo of the data I do the following:

  dtg_CutWeek.DataSource = ""; // ELIMINO LOS DATOS QUE CONTENGA
    dtg_CutWeek.DataSource = dt; // ASIGNO NUEVOS DATOS QUE ESTAN EN MI dt
    dtg_CutWeek.DataBind(); //HAGO EL BINDEO DE LA INFORMACION .. 

dtg_CutWeek IT IS THE NAME OF MY GRID IF YOU REALIZE IT IS THE ONE I USE TO REFERENCE ON THE BACKEND

    
answered by 07.03.2018 / 21:21
source
0

In the webform.aspx you should have something like this, where dgvData is the identifier of your GridView.

<asp:GridView ID="dgvDatos" runat="server" AutoGenerateColumns="True">
</asp:GridView>

In your codebehind webform.aspx.cs you should do something similar to this.

protected void Page_Load(object sender, EventArgs e)
    {
        dgvDatos.DataSource = new Persona().Listar();
        dgvDatos.DataBind();
    }

You can pass a Dataset from a BD, or direct a data collection as in this example.

    
answered by 07.03.2018 в 21:41