问题描述
是否可以绑定网格视图的各个列,
我的意思是,我在SQL Server 2008中编写了5个程序来计算和结果5个不同的值。现在我想将不同过程中的这些值绑定到不同列中的相同网格视图。
如果可能的话,请建议我这样做。
是否也可以执行网格视图单元格值的添加?
谢谢
Is it possible to bind individual columns of a grid view ,
What I mean is that , I have made 5 procedures in SQL server 2008 which computes and results in 5 different values.Now I want to bind these values from different procedures to the same grid view in different columns.
Please suggest me a way to do this , if this is possible.
Is it also possible to perform addition of grid view cell values?
Thanks
推荐答案
DataTable dt = new DataTable();
dt.Columns.Add("value1", typeof(int));
dt.Columns.Add("value2");
dt.Columns.Add("value3");
dt.Columns.Add("value4", typeof(DateTime));
DataRow row = dt.NewRow();
int value1 = CallStoreProcedure1();
string value2 = CallStoreProcedure2();
string value3 = CallStoreProcedure3();
DateTime value4 = CallStoreProcedure4();
row[0] = value1;
row[1] = value2;
row[2] = value3;
row[3] = value4;
gridView.DataSource = dt;
最后,您可以向此表添加行。但是你需要一些额外的工作来将新数据返回到数据库。
另外,代替一个表,你可以构建一个类o结构并构建一个列表:
Finally, you can add rows to this table. But you'll need some extra work to return the new data to the database.
Also, instead a table, you can build a class o struct and build a list:
class MyValues {
public int Value1 { get; set; }
public string Value2 { get; set; }
public string Value3 { get; set; }
public DateTime Value4 { get; set; }
}
并将其分配给网格视图:
and assign it to the grid view:
MyValues values = new MyValues();
values.Value1 = CallStoreProcedure1();
values.Value2 = CallStoreProcedure2();
values.Value3 = CallStoreProcedure3();
values.Value4 = CallStoreProcedure4();
List<MyValues> list = new List<MyValues>();
list.Add(values);
gridView.DataSource = list;
public int CallStoreProcedure1() {
using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.ConnectionString)) {
SqlCommand cmd = new SqlCommand("StoredProcedureName", conn);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
SqlParameter param = cmd.Parameters.Add(new SqlParameter("@MyParamName", System.Data.SqlDbType.Int));
param.Direction = System.Data.ParameterDirection.Output;
conn.Open();
cmd.ExecuteNonQuery();
return (int)cmd.Parameters["@MyParamName"].Value;
}
}
我假设你的程序有一个输出参数,并且总是返回一个整数值。如果程序可以返回空值,你应该检查它。
问候
Miguel
I have suposing that you procedure has an output parameter and allways are returning an integer value. If the procedure can return null values you should check it.
Regards
Miguel
这篇关于Asp.Net GridView问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!