2011-04-18 85 views
2

我是C#中的新成员。我想在运行时在GridView中添加行。我从2或3张表中收集数据。但是每当我去 用GridView绑定()它时,最后一个插入的行将被当前的一个覆盖。而GridView只显示当前行。 是否可以将两行显示在另一个的下方?或者是否有任何代码可以这么做。请向我推荐代码,以便我可以在我的项目中使用它。谢谢。GridView在运行时逐行添加

回答::首先你必须声明一个静态datatable.And一个布尔变量,其值初始为“true”。 然后执行以下代码--- >>> 这里是我的代码::

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

}

+0

从选择语句中的所需表格中选择数据或从t中单独选择数据他将所有结果列表并合并到一个列表中并将其绑定到网格 – 2011-04-18 06:54:05

+0

您现在正在做什么..需要查看代码 – V4Vendetta 2011-04-18 07:01:52

+0

@ V4Vendetta ::感谢您的帮助。在这里我正在给我的代码。 – Pavvy 2011-04-18 16:55:02

回答

1
//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

行添加到DataGridView的本身

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

感谢您的帮助。但我怎样才能给个别列值。在我的代码是有可能的吗?请看我上面的代码。 – Pavvy 2011-04-18 17:05:36

+0

在databind()后加上这个; – Nighil 2011-04-19 04:13:04

+0

@abatishchev ::我试过你的代码,但我正在使用web应用程序和webcontent窗体。因此你的方法不适合我。如果有任何解决方案,请帮助我。 – Pavvy 2011-04-19 09:54:22