如何将ds绑定到gridview? [英] how to bind ds to gridview ?

查看:76
本文介绍了如何将ds绑定到gridview?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是我的代码的一部分,

Hi,
this is a part of my code,

if (Session["MenuItem"] != null)
{
    string s = Session["MenuItem"].ToString();
    int TypeId = getTypeId(Session["MenuItem"].ToString());
    Adapter = new SqlDataAdapter("SELECT Title,Date,Time,AgeId FROM Subjects WHERE CId = '"+TypeId+"'", con);
    Adapter.Fill(ds, "Subjects");
    GridView1.DataSource = ds;
    GridView1.DataMember = "Subjects";
    GridView1.DataBind();
    GridView1.Visible = true;
    Label3.Text = ds.Tables[0].Rows.Count.ToString();
    //DataTable dt = ds.Tables[0];
    //Adapter.Fill(dt);
    //GridView1.DataSource = dt;
    //GridView1.DataBind();

}





和this for网格视图



and this for grid view

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False">
    </asp:GridView>





为什么gridview没有出现在页面中?



why gridview does not appear in the page ?

推荐答案

当我这样做...



command = new sqlcommand

using command

设置文字,类型,连接

结束







table = new datatable

adapter = new adapter

adapter.selectcommand = command



adapter.fill(表)





然后......



grid.datasource = table.defaultview

grid.databind



处理所有事情。



我的问题是为什么设置数据库?你填写了这张桌子。仅当您有多个数据源时才使用datamember。然后,您可以设置特定的表或集合。我错过了你的代码吗? :)我经常在ASP中找不到。
When I do this ...

command = new sqlcommand
using command
set text, type, connection
end



table = new datatable
adapter = new adapter
adapter.selectcommand = command

adapter.fill(table)


Then ...

grid.datasource = table.defaultview
grid.databind

Dispose of everything.

My question to you is why set the datamember? You filled with this table. Use datamember only if you have multiple sources of data. Then you can set the specific table or collection. Did I miss something in your code? :) I am not found very often in ASP.


首先检查,你的表有数据。



你可以绑定gridview使用数据集as,首先声明一个公共数据集,如



公共数据集ds;



First check, your table has data.

You can bind gridview with dataset as, first declare a public dataset like

public dataset ds;

string s = Session["MenuItem"].ToString();
int TypeId = getTypeId(Session["MenuItem"].ToString());
Adapter = new SqlDataAdapter("SELECT Title,Date,Time,AgeId FROM Subjects WHERE CId = '"+TypeId+"'", con);
Adapter.Fill(ds, "Subjects")
  GridView1.DataBind();;










<asp:GridView DataSource='<%#ds>' ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False">
    </asp:GridView>





你可以在源头注意到我有为gridview添加了DataSource属性



试试这个。



as you can notice in source i have added DataSource property for gridview

Try this.


首先检查,你的表有数据。



您可以将gridview与数据集绑定为,首先声明一个公共数据集,例如



公共数据集ds;



First check, your table has data.

You can bind gridview with dataset as, first declare a public dataset like

public dataset ds;

string s = Session["MenuItem"].ToString();
int TypeId = getTypeId(Session["MenuItem"].ToString());
Adapter = new SqlDataAdapter("SELECT Title,Date,Time,AgeId FROM Subjects WHERE CId = '"+TypeId+"'", con);
Adapter.Fill(ds, "Subjects")
  GridView1.DataBind();;










<asp:GridView DataSource='<%#ds>' ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False">
    </asp:GridView>





你可以在源头注意到我有为gridview添加了DataSource属性



试试这个。



as you can notice in source i have added DataSource property for gridview

Try this.


这篇关于如何将ds绑定到gridview?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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