Asp.Net GridView问题 [英] Asp.Net GridView Problem

查看:68
本文介绍了Asp.Net GridView问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以绑定网格视图的各个列,



我的意思是,我在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(我没有编译就写了。

Hello
You sould make same work before binding to the grid:
For example, you can build a DataTable with one column for each value (I have writen without compile.
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;


在这些日子里,有很多方法可以访问数据库:实体框架,nHibernate,基本的ado.net ...我会直接使用ado.net最基本的



一个例子可能是:



In these days there are many ways to access to a database: entity framework, nHibernate, basic ado.net... I'll supose the most basic using ado.net directly

An example could be:

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问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆