从页面加载事件中获取数据并在ASP.NET中的gridview上显示 [英] Get data from page load event and display on gridview in ASP.NET
问题描述
您好,
我在页面加载事件中将一些数据存储到变量中。我在同一页面上有一个gridview,它从SQL数据库中获取数据。我想要的是在gridview中添加另一列,并使用Page Load事件中变量中的值填充它。
有什么办法吗?我想这样做是因为我正在计算一些会因日期变化而改变的东西,因此我想从页面加载中获取数据并用它填充gridview。
我尝试过:
搜索谷歌和其他论坛但找不到与此相关的任何内容。
像GridView
这样的数据表示控件只能处理一个DataSource
一次。由于您的数据来自不同的来源(数据库和代码变量),因此您可能需要创建一个自定义的DataTable
,它将包含您所需的所有信息。然后,您可以使用该自定义DataTable
作为GridView $ c的最终
DataSource
$ c>。
这是一个简单的例子:(PS:从未在此测试,但它应该给你一些想法)
< pre lang =c#> private void BindGrid(DataDaTable source)
{
// 创建最终数据源
DataTable dt = new DataTable();
DataRow博士;
// 定义您需要的新列集
dt.Columns.Add( new System.Data.DataColumn( Column1, typeof ( String )));
dt.Columns.Add( new System.Data.DataColumn( Column2, typeof ( String )));
dt.Columns.Add( new System.Data.DataColumn( Column3, typeof ( String )));
// 如果需要,您可以在此处添加更多列
// 使用来自您的来源的数据填充新的DataTable
int rowCount = source.Rows.Count;
if (rowCount > 0 ){
for ( int i = 0 ; i < rowCount; i ++){
dr = dt.NewRow();
dr [ 0 ] = source.Rows [i] [ 的ColumnName跨度>]的ToString();
dr [ 1 ] = source.Rows [i] [ 的ColumnName跨度>]的ToString();
dr [ 2 ] = 您的自定义这里的变量值;
dt.Rows.Add(dr);
}
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
或者,你可以添加一个新的列
到您现有的DataTable
类似于:
DataColumn newColumn = new DataColumn( ColumnName, typeof (System. String ));
newColumn.DefaultValue = 您的变量值;
dt.Columns.Add(newColumn);
我试过下面
使用系统;
使用 System.Collections.Generic;
使用 System.Data;
使用 System.Linq;
使用 System.Web;
使用 System.Web.UI;
使用 System.Web.UI.WebControls;
命名空间 WebApplication1
{
public partial class WebForm3:System.Web.UI.Page
{
string customColumn = string .Empty;
受保护 void Page_Load( object sender,EventArgs e)
{
customColumn = INDIA;
}
受保护 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的));
// 这里我们添加五个DataRows。
表。 Rows.Add( 25 , Indocin, David,DateTime.Now);
table.Rows.Add( 50 , Enebrel, Sam,DateTime.Now);
table.Rows.Add( 10 , 肼苯哒嗪, Christoff,DateTime.Now);
table.Rows.Add( 21 , Combivent, Janet,DateTime.Now);
table.Rows.Add( 100 , Dilantin, Melanie,DateTime.Now);
// 如果未对autogenerare设置为true,请在gridview中添加新列列
// BoundField test = new BoundField();
// test.DataField =Country;
// test.HeaderText =Country;
// GridView1.Columns.Add(test);
// 将自定义变量的新列添加到数据源
table.Columns.Add( new DataColumn( Country));
GridView1.DataSource = table;
GridView1.DataBind();
}
protected void GridView1_RowDataBound(< span class =code-keyword> object sender,GridViewRowEventArgs e)
{
if (e.Row.RowType = = DataControlRowType.DataRow)
{
// 将数据放入Conditionaly列
if (Convert.ToDateTime(e.Row.Cells [ 3 ]。文本)< DateTime.Now)
{
e.Row.Cells [ 4 ]。Text = customColumn;
}
}
}
}
}
Hello,
I am storing some data into a variable at Page Load event. And I have a gridview in the same page which is getting data from SQL database. What i want is to also add another column in the gridview and populate it with the value in the variable from Page Load event.
Is there any way for this? I want to do this because i am calculating something which will be changed with respect to change in date, so therefore i want to fetch data from page load and populate the gridview with it.
What I have tried:
Searched google and other forums but could not find anything related to this.
A data representation control likeGridView
can only handle oneDataSource
at a time. Since your data is coming from different sources (database and code variable), then you may need create a customDataTable
that would holds all the information you need from your sources. You could then use that customDataTable
as your finalDataSource
for yourGridView
.
Here's a quick example: (PS: Never tested on this, but it should give you some idea)
private void BindGrid(DataDaTable source) { //create your final datasource DataTable dt = new DataTable(); DataRow dr; //define new sets of columns that you need dt.Columns.Add(new System.Data.DataColumn("Column1", typeof(String))); dt.Columns.Add(new System.Data.DataColumn("Column2", typeof(String))); dt.Columns.Add(new System.Data.DataColumn("Column3", typeof(String))); //you can add more columns here if needed //fill your new DataTable with data from your sources int rowCount = source.Rows.Count; if ( rowCount > 0){ for(int i=0; i < rowCount; i++){ dr = dt.NewRow(); dr[0] = source.Rows[i]["ColumnName"].ToString(); dr[1] = source.Rows[i]["ColumnName"].ToString(); dr[2] = "Your custom variable value here"; dt.Rows.Add(dr); } } GridView1.DataSource = dt; GridView1.DataBind(); }
Alternatively, you could just add a newColumn
to your existingDataTable
something like:
DataColumn newColumn = new DataColumn("ColumnName", typeof(System.String)); newColumn.DefaultValue = "Your variable value"; dt.Columns.Add(newColumn);
I have tried below
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 = "INDIA"; } 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; } } } } }
这篇关于从页面加载事件中获取数据并在ASP.NET中的gridview上显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!