开发者

Adding row by row in GridView in runtime

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();           

}


  //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();     


add rows to DataGridView itself

DataGridViewRow row = new DataGridViewRow();
dataGridView1.BeginEdit();
//your columns
row.Cells["columnOne"] = valueofone;
row.Cells["columnTwo"] = valueoftwo;
dataGridView1.Rows.Add(row);
dataGridView1.EndEdit();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜