1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to add row to gridview at the run time

Discussion in 'C#' started by adexpert10, Oct 26, 2010.

  1. #1
    How to add row to gridview at the run time in asp.net? I am using c# as a language. Please help me regarding this.
     
    adexpert10, Oct 26, 2010 IP
  2. Bigcats82

    Bigcats82 Greenhorn

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #2
    This should help you out.

    codeproject.com/KB/custom-controls/NewRowGridView.aspx
     
    Bigcats82, Oct 26, 2010 IP
  3. AstarothSolutions

    AstarothSolutions Peon

    Messages:
    2,680
    Likes Received:
    77
    Best Answers:
    0
    Trophy Points:
    0
    #3
    It depends on what you are trying to acheive.

    You can either add the row to the datatable/ dataset before binding it to the gridview or add it to the gridview after the data has been bound (if your wanting it to appear on every page if your using paging)
     
    AstarothSolutions, Oct 27, 2010 IP
  4. adexpert10

    adexpert10 Peon

    Messages:
    104
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I am making online billing system and I want to add more than one items and I want to use gridview control
     
    adexpert10, Oct 27, 2010 IP
  5. nikes

    nikes Peon

    Messages:
    48
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    You can add any number of data to gridview. First you collect data to a datatable and bind it to gridview
     
    nikes, Oct 28, 2010 IP
  6. prkishnani

    prkishnani Peon

    Messages:
    164
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Hi............
    The following snippet demonstrates adding of row to grid view programmatically at run-time. just learn it ....it will help you..

    Name spaces to used are:
    using System.Data.SqlClient;
    using System.Data;

    Step1:

    The following code should be coded under page load event



    SqlConnection cn = new SqlConnection("user id=admin;password=admin123;data source=ganesh1");
    SqlDataAdapter da = new SqlDataAdapter("select * from emp", cn);
    ds = new DataSet();
    da.Fill(ds, "temp");
    GridView1.DataSource = ds.Tables[0];
    GridView1.DataBind();



    Step2:

    The following code should be coded under row created event of grid view




    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    { //checking whether rowtype is datacontrolrowtype
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
    double annsal;
    //getting the required data from database
    annsal =double .Parse ( ds.Tables[0].Rows[e.Row.RowIndex]["sal"].ToString()) * 12;
    //initializing and adding label control to gridview

    Label l1=new Label();

    l1.Text =annsal .ToString ();
    e.Row .Cells [1].Controls .Add (l1);

    //checking tha annsal and drawing the row with particular color

    if (annsal > 1000)
    {
    e.Row.BackColor = System.Drawing.Color.Red;

    }
    else
    e.Row.BackColor = System.Drawing.Color.Gray;
    }
    }



    Step 3: Customize the gridview.Use 2 templates
    Template 1-->
    A) Header Text -->Employee Name

    B) Place Label in Item Template and bind Text to Eval("Ename")
    Template2-->
    A) Header Text=Annual Salary

    Set Auto Generate fields to False

    Step 4: Execute

    Explanation
    -->The grid view is binded with the database
    -->using row created event of grid view add the row to grid view at runtime


    Web Designer | Real Estate Web Design
     
    prkishnani, Nov 8, 2010 IP