Vinicius Cano
Vinicius Cano

Reputation: 319

Asp net Populate a Grid with table

I need to create a gridview with the content of a table,i can't use the option in gridview tasks to choose a data source,need to do by code: This is my GridView:

<asp:GridView ID="gridTStatus" runat="server" AutoGenerateColumns="False"  OnPageIndexChanging="gridTStatus_PageIndexChanging"
                                >
                                <Columns>
                                   <asp:BoundField DataField="iClic" HeaderText="Clic" SortExpression="iClic" /> 
                                    <asp:BoundField DataField="iStatus" HeaderText="Status" 
                                        SortExpression="iStatus" />
                                    <asp:BoundField DataField="dtDateCreated" HeaderText="Data de Criação" 
                                        SortExpression="dtDateCreated" />
                                    <asp:BoundField DataField="iEDV" HeaderText="EDV" SortExpression="iEDV" />
                                    <asp:BoundField DataField="sComments" HeaderText="Comentários" 
                                        SortExpression="sComments" />
                                </Columns>
                            </asp:GridView> 

I have a table that have all the status need to populate,what is the best way to populate this grid?

UPDATE

I created this method that use the procedure,now to read the information from the table,i need to use the dataReader ?

public Status SelectStatusClic(Clic objClic)
        {
            Status returnStatus;

            const string strStoredProcedure = "spSearchClicStatus";

            try
            {
                Database database = DatabaseFactory.CreateDatabase(DATABASESETTINGS.CLICDB);
                using (DbCommand dbCommand = database.GetStoredProcCommand(strStoredProcedure))
                {
                    if ((objClic != null) && (objClic.ID != 0))
                    {
                        database.AddInParameter(dbCommand, "@iClicID", DbType.Int32, objClic.ID);
                    }
                    database.ExecuteNonQuery(dbCommand);
                }
                returnStatus = new Status();
            }

            return returnStatus;
        }

Upvotes: 0

Views: 583

Answers (1)

Vitor Canova
Vitor Canova

Reputation: 3976

In the code the best way to populate the grid I can think is create a colletion of objects that represents your data and bind to the grid:

gridTStatus.DataSource = collectionOfStatuses;
gridTStatus.DataBind();

Where collectionOfStatuses can be a list of objects with the data you need to bind. Like this:

var collectionOfStatuses = new List<Status>();
collectionOfStatuses.Add(new Status() { iClic = true });

If you don't know how to retrieve data from database you can follow this answer. (If this is the case your question is a duplicate)

Upvotes: 1

Related Questions