[RESOLVED] Adding rows to GridView programatically
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: [RESOLVED] Adding rows to GridView programatically

  1. #1
    Join Date
    Mar 2005
    Location
    マレーシア
    Posts
    155

    [RESOLVED] Adding rows to GridView programatically

    Today is the first day i hav to use the new ASP.NET 2.0 GridView control. After searching all over the place, it seems that one cannot programatically add rows to GridView like one can with DataGrid. Why can`t i add rows programatically? And is it possible with some trick to add rows? Makes me feel like wanting to go back to using DataGrid
    Ai n mai speling sks, bt ai du it mainfuli
    http://www.geocities.jp/juuhassai/i_rouseiji.html

  2. #2
    Join Date
    Dec 2003
    Location
    Northern Ireland
    Posts
    1,362

    Re: Adding rows to GridView programatically

    You can populate a 'DataTable' with your data then bind that to the gridview (you can programatically add rows to the datatable).

    i.e

    Code:
    Imports System.Data
    
    Private Sub BindData()
        gridview1.DataSource = GetData()
        gridview1.DataBind()
    End Sub
    
    Protected Function FormatDataTable() as DataTable
        Dim dt as DataTable = New DataTable()
        ' Create Columns
        dt.Columns.Add("id", System.Type.GetType("System.String"))
        dt.Columns.Add("text", System.Type.GetType("System.String"))
        Return dt
    End Function
    
    Protected Function GetData() as DataTable
        Dim dt as DataTable = FormatDataTable()
        Dim dr as DataRow
    
        ' Populate the datatable with your data (put this in appropriate loop)
        dr = dt.NewRow
        dr("id") = "1"
        dr("txt") = "Hi"
        ' Add the row
        dt.Rows.Add(dr)
    
        dt.AcceptChanges
        Return dt
    End Function
    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rich Cook


    0100 1101 0110 1001 0110 0011 0110 1000 0110 0001 0110 0101 0110 1100 0010 0000 0100 0101 0110 1100 0110 1100 0110 0101 0111 0010

  3. #3
    Join Date
    Mar 2007
    Posts
    1

    Resolved Re: Adding rows to GridView programatically

    this's the link
    http://www.codeproject.com/aspnet/NewRowGridView.asp

    you can use a botton and add rows

    if you change this parth:

    public void CreateNewRows()
    {

    if (this.NewRowCount > 0)
    {
    ArrayList list = new ArrayList();
    DataControlField[] fields = this.GetDataControlFields();
    for (int i = 0; i < this.NewRowCount; i++)
    {
    GridViewRow newRow = this.CreateNewRow(i, fields);
    list.Add(newRow);
    }
    this._newRows = new GridViewRowCollection(list);
    Table grid;
    int rowIndex;
    if (this.Rows.Count == 0)
    {
    grid = this.CreateChildTable();
    this.Controls.Add(grid);
    if (this.ShowHeader)
    {
    GridViewRow headerRow = this.CreateHeaderRow(fields);
    grid.Rows.Add(headerRow);
    }
    rowIndex = this.Rows.Count + 1;

    }
    else
    {


    grid = this.CreateChildTable();






    rowIndex = 0;
    }


    foreach (GridViewRow newRow in this._newRows)
    {
    grid.Rows.AddAt(rowIndex, newRow);
    rowIndex++;
    }

    this.Controls.Add(grid);
    }
    }

    in c#:

    public static int intContador; (attribute)

    protected void btnAgregar_Click(object sender, ImageClickEventArgs e)
    {
    intContador++;
    GridView1.NewRowCount = intContador;
    }

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center