C# 我想在页面加载事件中将数据表中的列添加到gridview:

C# 我想在页面加载事件中将数据表中的列添加到gridview:,c#,asp.net,gridview,datatable,webforms,C#,Asp.net,Gridview,Datatable,Webforms,我尝试从datatable向gridview添加列(我从sql数据库填充),我调试了它,我的网格视图有列,但在web端它根本不显示我的网格视图 SqlConnection conn = new SqlConnection( new SqlConnectionStringBuilder() { DataSource = "HOSEIN-PC", InitialCatalo

我尝试从datatable向gridview添加列(我从sql数据库填充),我调试了它,我的网格视图有列,但在web端它根本不显示我的网格视图

            SqlConnection conn = new SqlConnection(
           new SqlConnectionStringBuilder()
           {
               DataSource = "HOSEIN-PC",
               InitialCatalog = "project Grid",
               UserID = "sa",
               Password = "fast"
           }.ConnectionString);
            conn.Open();
            //ستون جدول
            string SqlString = string.Format(@"select *
        from tbl_data ");
            SqlDataAdapter sda = new SqlDataAdapter(SqlString, conn);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            //GridView1.DataSource = dt;
            //GridView1.DataBind();

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                BoundField c = new BoundField();
                //GridView1.Columns.Add(dt.Rows[i]["DATA"].ToString());
                 // it doesnt work in webform so i try boundfield
                c.HeaderText = dt.Rows[i]["DATA"].ToString();
                c.DataField = dt.Rows[i]["ID"].ToString();
                GridView1.Columns.Add(c);
            }
            GridView1.DataBind();

            conn.Close();
SqlConnection conn=新的SqlConnection(
新的SqlConnectionStringBuilder()
{
DataSource=“HOSEIN-PC”,
InitialCatalog=“项目网格”,
UserID=“sa”,
Password=“fast”
}.连接字符串);
conn.Open();
//ستون جدول
字符串SqlString=string.Format(@)选择*
来自tbl_数据);
SqlDataAdapter sda=新的SqlDataAdapter(SqlString,conn);
DataTable dt=新的DataTable();
sda.填充(dt);
//GridView1.DataSource=dt;
//GridView1.DataBind();
对于(int i=0;i
看起来没问题,我在网格视图中有列。。。 唯一的问题是当我执行它时,它不会在web端显示网格视图。

我在下面尝试过

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class WebForm3 : System.Web.UI.Page
    {
        string customColumn = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            customColumn = "AAA";
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            DataTable table = new DataTable();
            table.Columns.Add("Dosage", typeof(int));
            table.Columns.Add("Drug", typeof(string));
            table.Columns.Add("Patient", typeof(string));
            table.Columns.Add("Date", typeof(DateTime));

            // Here we add five DataRows.
            table.Rows.Add(25, "Indocin", "David", DateTime.Now);
            table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
            table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
            table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
            table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);


            //Add new column  in gridview if its not set true for autogenerare columns
            //BoundField test = new BoundField();
            //test.DataField = "Country";
            //test.HeaderText = "Country";
            //GridView1.Columns.Add(test);


            //add  new column for your custom variable to datasource
            table.Columns.Add(new DataColumn("Country"));


            GridView1.DataSource = table;
            GridView1.DataBind();

        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //putting data in column Conditionaly 
                if (Convert.ToDateTime(e.Row.Cells[3].Text)<DateTime.Now)
                {
                    e.Row.Cells[4].Text = customColumn;
                }
            }
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统数据;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
命名空间WebApplication1
{
公共部分类WebForm3:System.Web.UI.Page
{
string customColumn=string.Empty;
受保护的无效页面加载(对象发送方、事件参数e)
{
customColumn=“AAA”;
}
受保护的无效按钮1\u单击(对象发送者,事件参数e)
{
DataTable=新的DataTable();
表.列.添加(“剂量”,类型(int));
表.列.添加(“药物”,类型(字符串));
表.列.添加(“患者”,类型(字符串));
表.列.添加(“日期”,类型(日期时间));
//这里我们添加五个数据行。
表.Rows.Add(25,“Indocin”,“David”,DateTime.Now);
添加(50,“Enebrel”,“Sam”,DateTime.Now);
表.Rows.Add(10,“Hydrazine”,“Christoff”,DateTime.Now);
添加(21,“Combivent”,“Janet”,DateTime.Now);
表.行.添加(100,“迪兰丁”,“梅勒妮”,日期时间.现在);
//如果未为autogenerare列设置为true,则在gridview中添加新列
//BoundField测试=新的BoundField();
//test.DataField=“Country”;
//test.HeaderText=“国家”;
//GridView1.Columns.Add(测试);
//将自定义变量的新列添加到数据源
表.Columns.Add(新数据列(“国家”));
GridView1.DataSource=表格;
GridView1.DataBind();
}
受保护的void GridView1_RowDataBound(对象发送方,GridViewRowEventArgs e)
{
如果(e.Row.RowType==DataControlRowType.DataRow)
{
//有条件地将数据放入列中

如果(Convert.ToDateTime(e.Row.Cells[3].Text),您需要在
GridView1.DataSource=dt;
之前执行
GridView1.DataBind();
不需要手动添加列…如果要显示datatable的所有列,只需将GridView的
AutoGenerateColumns
属性设置为false,并通过执行
GridView1.DataSource=dt;GridView1.DataBind()将datatable绑定到GridView;
如果您想在GridView中只显示dataTable中的特定列,那么您需要将
AutoGenerateColumns
属性设置为false并使用
,但我需要将列从数据库添加到网格视图中……如果我使用gridview1.datasource,它将无法按我的意愿工作。@ChetanRanpariyaoh tnx我将尝试(自动生成)@ChetanRanpariya