Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4550228
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T16:28:06+00:00 2026-05-21T16:28:06+00:00

I am new in C#.I want to add rows in a GridView in runtime.

  • 0

I am new in C#.I want to add rows in a GridView in runtime. I collect a data from 2 or 3 tables. But whenever I am going to
bind() it with GridView, the last inserted row is overwritten by current one. And GridView shows only the current row.
Is it possible to show both rows one bellow the other? Or Is there any code for doing so.Please suggest me code for that so that i can use it in my project.Thanks.

Answer::First you have to declare a static datatable.And a boolean variable having value initially “true”.
And then execute following code—>>>
Here is My code::

protected void btnAdd_Click(object sender, EventArgs e)
{
    int coursemasterid = Convert.ToInt32(dlAdmissionCourses.SelectedItem.Value);
    int batchmasterid = Convert.ToInt32(dlAssignBatch.SelectedItem.Value);


    string SQL1 = "SELECT coursename,coursefees,batchname FROM  CourseMaster,BatchMaster WHERE CourseMaster.coursemasterid=BatchMaster.coursemasterid and CourseMaster.coursemasterid="+coursemasterid+" and BatchMaster.batchmasterid="+batchmasterid+"";
      DataTable otable = new DataTable();
        otable = DbHelper.ExecuteTable(DbHelper.CONSTRING, CommandType.Text, SQL1, null);
        DataRow dr1 = otable.Rows[0];
        string coursename = dr1["coursename"].ToString();
        int coursefees = Convert.ToInt32(dr1["coursefees"]);
        string batchname = dr1["batchname"].ToString();

if (chkadd == true)
        {
            dtglb = new DataTable();  //here dtglb is a global datatable
            dtglb.Columns.Add("coursename", typeof(string));
            dtglb.Columns.Add("coursefees", typeof(int));
            dtglb.Columns.Add("batchname", typeof(string));

        }
        foreach (DataRow dr in otable.Rows)
        {
            dtglb.NewRow();
            dtglb.Rows.Add(coursename,coursefees,batchname);

        }
        chkadd = false;
        GridView1.DataSource = dtglb;
    GridView1.DataBind();           

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T16:28:07+00:00Added an answer on May 21, 2026 at 4:28 pm
      //declaring a datatable global in form 
            DataTable dtglb=new DataTable();
    
            //In click event    
        SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=EMS;User ID=sa;Password=sa123");
    
        string SQL1 = "SELECT coursename,coursefees,batchname FROM  CourseMaster,BatchMaster WHERE CourseMaster.coursemasterid=BatchMaster.coursemasterid and CourseMaster.coursemasterid="+coursemasterid+" and BatchMaster.batchmasterid="+batchmasterid+"";
        SqlCommand cmd = new SqlCommand(SQL1, con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable ds = new DataTable();
        //DataColumn faculty = new DataColumn();
        da.Fill(ds);
        GridView1.DataSourceID = null;
    
        //New Code Added Here
        DataRow row = ds.NewRow();
        //your columns
        row["columnOne"] = valueofone;
        row["columnTwo"] = valueoftwo;
        dtglb.Rows.Add(row);
            foreach(DataRow dr in dtglb.Rows)
        {
         ds.Rows.Add(dr);
        }
        //=========
        GridView1.DataSource = ds;
        GridView1.DataBind();     
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to add a new filter to the Project Explorer, to hide from
I generated Personel[] type array from Biz.Bal.GetPersonelById(1) but if I want to add winforms,
I have a gridview and I want to add rows in it dynamically. I
I have a GridView with a header and some rows and want to add
I want add new Feed item on entity persist and update. I write this
I want to add new obj of JSON like: 128: { Msg: [{ me:
I want to add a new property bool IsValid to my class below. I
I want to add a new row to my JSON when the user clicks
I want to add a new tab page for every newly opened form. Example:
I want to add a new feature in a wxPython class. I also want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.