在aspx页面中使用不同的数据表绑定gridview itemtemplate [英] Bind gridview itemtemplate with different datatable in aspx page

查看:64
本文介绍了在aspx页面中使用不同的数据表绑定gridview itemtemplate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有与数据表绑定的gridview。这个数据表填充了包含3个select语句(返回计数)和where子句的SQL过程。我需要在标签上的gridview itemtemplate中的3select语句中显示这些count的返回。这必须在使用eval<%count1%>的aspx页面中完成。

请帮助,asp gridview的新手。 />


我尝试过:



绑定gridvw.dataset();

在aspx页面中gridview itemtemplate

I have gridview that bind with data table.This datatable is fill with SQL procedure that contain 3 select statement(return count's) with where clause. I need to show these count's return from 3select statement inside gridview itemtemplate on labels.This must be done in aspx page using eval<%count1%>.
Please Help , new to asp gridview.

What I have tried:

Bind gridvw.dataset();
In aspx page gridview itemtemplate

推荐答案

GridView 这样的数据表示控件只能一次处理一个 DataSource 。由于您的数据来自不同的表,因此您可能需要加入这些表,以便从连接的表中选择列,并将其作为SP的最终结果。或者,您可以查询这些表,就像您当前所做的那样,并在您的代码中创建一个自定义 DataTable ,它将保存两个表中所需的所有列。然后,您可以使用该自定义 DataTable 作为最终 DataSource



这是一个简单的例子:



A data representation control like GridView can only handle one DataSource at a time. Since your data is coming from different tables, then you may need to join those table so you can select columns from the joined tables and make that as the final result from you SP. Alternatively, you can query those tables, just like what you did currently and create a custom DataTable in your code that would holds all the columns you need from the two tables. You could then use that custom DataTable as your final DataSource.

Here's a quick example:

private void BindGrid(DataSet ds)
{
        DataTable dt = new DataTable();
        DataRow dr;
        dt.Columns.Add(new System.Data.DataColumn("MarkedCount", typeof(String)));
        dt.Columns.Add(new System.Data.DataColumn("LegalCount", typeof(String)));
        dt.Columns.Add(new System.Data.DataColumn("NotLegalCount", typeof(String)));

        DataTable firstResult = ds.Tables[0];
	DataTable secondResult = ds.Tables[1];
	DataTable thirdResult = ds.Tables[2];

	if (firstResult.Rows.Count > 0){
            dr = dt.NewRow();
            dr[0] = firstResult.Rows[0][0].ToString();
            dr[1] = secondResult.Rows[0][0].ToString();
	    dr[2] = thirdResult.Rows[0][0].ToString();
            dt.Rows.Add(dr);
        }

	GridView1.DataSource = dt;
        GridView1.DataBind();
}





DataSet 参数将保存您的数据SP。一旦你有了这个,你应该能够使用从自定义 DataTable 定义的列,并使用它来绑定你的控件在 ItemTemplate





The DataSet parameter will hold the data from your SP. Once you've got that, you should be able to use the columns defined from the custom DataTable, and use that to bind your Control within ItemTemplate.

<itemtemplate>
        <asp:label id="Label1" runat="server" text="<%# Bind("MarkedCount") %>">
</asp:label></itemtemplate>

这篇关于在aspx页面中使用不同的数据表绑定gridview itemtemplate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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