CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2007
    Posts
    39

    Populating a Gridview

    Hi there,

    I'm building a web site using C# in visual studio and I'm using a SQL server 2005 database.

    I have several declared variables which are assigned variables at runtime. However i wish to view these values in a gridview for editing by the user before i write the values to the database. At runtime i want to populate the gridview with several rows of data before giving the user the opportunity to delete/modify the contents before i commit the data to my database.
    Any help would be much appreciated,

    Thanks,

    Adrian.

  2. #2
    Join Date
    Dec 2002
    Location
    St.Louis MO, USA
    Posts
    672

    Re: Populating a Gridview

    U can make DataTable,DataColumn and DataRows as follows and then assign to ur grid.
    Code:
    private void MakeDataTableAndDisplay(){
       // Create new DataTable.
       DataTable myDataTable = new DataTable("MyDataTable");
       
       // Declare DataColumn and DataRow variables.
       DataColumn myDataColumn;
       DataRow myDataRow;
    
       // Create new DataColumn, set DataType, ColumnName and add to DataTable.    
       myDataColumn = new DataColumn();
       myDataColumn.DataType = System.Type.GetType("System.Int32");
       myDataColumn.ColumnName = "id";
       myDataTable.Columns.Add(myDataColumn);
    
       // Create second column.
       myDataColumn = new DataColumn();
       myDataColumn.DataType = Type.GetType("System.String");
       myDataColumn.ColumnName = "item";
       myDataTable.Columns.Add(myDataColumn);
    
       // Create new DataRow objects and add to DataTable.    
       for(int i = 0; i < 10; i++){
          myDataRow = myDataTable.NewRow();
          myDataRow["id"] = i;
          myDataRow["item"] = "item " + i;
          myDataTable.Rows.Add(myDataRow);
       }
       // Set to DataGrid.DataSource property to the table.
       dataGrid1.DataSource = myDataTable;
    }
    A Person who is polite is given goodness and a person who is away from Politeness is away from Goodness.

    NAUMAAN

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured