c# - 在运行时在 GridView 中逐行添加

标签 c# .net ado.net

我是 C# 的新手。我想在运行时在 GridView 中添加行。我从 2 或 3 个表中收集数据。但每当我要去 bind() 它与 GridView,最后插入的行被当前行覆盖。而 GridView 只显示当前行。 是否可以同时显示两行?或者是否有这样做的任何代码。请给我建议代码,以便我可以在我的项目中使用它。谢谢。

回答::首先您必须声明一个静态数据表。和一个初始值为“true”的 bool 变量。 然后执行下面的代码--->>> 这是我的代码::

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

关于c# - 在运行时在 GridView 中逐行添加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5699423/

相关文章:

c# - 如何在 C# 中编码集合以传递给 native (C++) 代码

c# - 不以某些文字开头的 ASP.NET MVC 路由

c# - 已存在文件的概率 System.IO.Path.GetRandomFileName()

c# - 将DataTable中的数据插入到数据库表中

c# - DbCommand 和参数化 SQL,ORACLE 与 SQL Server

c# - 调用未正确更新 GUI

c# - 如何使用 .NET 应用 Windows 组策略?

c# - 一个或两个 MVC 项目?

c# - 长时间运行的查询 Web 应用程序( azure )解决方案

entity-framework - 是否可以在 VS2010 中检索 DDL 生成模板内的连接字符串?