怎么把查询出来的数据显示在gridview中。

t240034137 2009-09-04 04:23:24
我想从数据库中的一个表中查询出来的信息,想显示在一个gridview种。怎么显示呀?帮帮我
...全文
1748 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
sngzer 2009-12-12
  • 打赏
  • 举报
回复
access数据库怎么显示呢?sql的我会
zw946 2009-09-05
  • 打赏
  • 举报
回复
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Bind();
}
}
protected void Bind()
{
SqlConnection con = new SqlConnection("server=.;database=northwind;uid=sa;pwd=");
string cmd ="select * from employees";
SqlDataAdapter sda = new SqlDataAdapter(cmd,con);
DataSet ds = new DataSet();
sda.Fill(ds,"emp");
GridView1.DataSource = ds.Tables["emp"];
GridView1.DataKeyNames = new string[] { "employeeid","city"};
GridView1.DataBind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{//编辑事件
//设置当前所编辑的行的索引,e.NewEditIndex是点击编辑按钮行的索引
GridView1.EditIndex = e.NewEditIndex;
//重新绑定
Bind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{//取消编辑事件
//设置当前所编辑的行的索引为-1
GridView1.EditIndex = -1;
Bind();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{//删除事件
//要删除某行,则需要唯一能标示这一行的值(例如:主键)作为删除的条件
string id = GridView1.DataKeys[e.RowIndex]["employeeid"].ToString();
string city = GridView1.DataKeys[e.RowIndex]["city"].ToString();
Response.Write(id+"<br/>"+city);
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{//更新事件
//类似于删除事件,同样需要获取值作为更新的条件.同时还需要获取要更新的内容
//获取主键
string id = GridView1.DataKeys[e.RowIndex]["employeeid"].ToString();
Response.Write("要删除的行的编号为: "+id);
//获取要更新的内容
string lastname = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text;
string firstname = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text;
Response.Write("你将把姓更新为"+lastname+",名更新为"+firstname);

//更新的SQL语句
string cmdStr = "update employees set lastname=" + lastname + ",firstname=" + firstname + " where employeeid=" + id;
//更新过程省略
}
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{//创建行事件
//判断当前行是否是数据行,e.Row即为当前行
if (e.Row.RowType == DataControlRowType.DataRow)
{//如果当前行是数据行则为该行添加两个特性
//鼠标悬停事件(onmouseover),鼠标移开事件(onmouseout);
//当鼠标悬停在当前行上时,将当前的颜色保存在c中,并为当前行赋予新颜色
e.Row.Attributes.Add("onmouseover","c=style.backgroundColor;style.backgroundColor='#00Eeff'");
//当鼠标移开当前行时,再将保存了该行原颜色的c赋给当前行
e.Row.Attributes.Add("onmouseout","style.backgroundColor=c");
}
}
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{//选择行事件
int index = e.NewSelectedIndex;
Response.Write("你选择了第"+index.ToString()+"行");
}
}
wsy9901 2009-09-04
  • 打赏
  • 举报
回复
dataset ds =new dataset();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
cmd.ExecuteNonQuery();
sda.Fill(ds);

Gv.DataSource = ds.table[0].DefaultView;
Gv.DataBind();
ygp285369675 2009-09-04
  • 打赏
  • 举报
回复
可以再页面的page_load加载事件里写相应的代码:

假设datagriedview控件的Id为showData
protected void page_load()
{

//实例化SqlConnection对象
SqlConnection sqlCon = new SqlConnection();
//实例化SqlConnection对象连接数据库的字符串
sqlCon.ConnectionString = "server=.;uid=sa;pwd=;database=SearchDB";
//定义SQL语句
string SqlStr = "select * from Discussion";
//实例化SqlDataAdapter对象
SqlDataAdapter da = new SqlDataAdapter(SqlStr, sqlCon);
//实例化数据集DataSet
DataSet ds = new DataSet();
da.Fill(ds, "Discussion");
//设置datagriedView控件的数据源
showData.DataSource=ds.Tables[0];
//调用DataBind方法显示数据
showData.DataBind();

}
hui_1019 2009-09-04
  • 打赏
  • 举报
回复
SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings("dbconnectionstring"));
SqlCommand cmd = new SqlCommand("select * from tb where 商品名称=Keyword.Text", conn);
conn.Open();

SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
ds.Fill(da);
gridview1.DataSource = ds;
gridview1.DataBind();
ds.Clear();
金大哈 2009-09-04
  • 打赏
  • 举报
回复

//一般用这个 ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings("dbconnectionstring"));
SqlCommand cmd = new SqlCommand("select * from tb where 商品名称=Keyword.Text", conn);
conn.Open();



dataset ds =new dataset();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
cmd.ExecuteNonQuery();
sda.Fill(ds);

Gv.DataSource = ds.table[0].DefaultView;
Gv.DataBind();
kingboyrang 2009-09-04
  • 打赏
  • 举报
回复

public void GridViewBind()//gridvie数据绑定方法
{
//实例化SqlConnection对象
SqlConnection sqlCon = new SqlConnection();
//实例化SqlConnection对象连接数据库的字符串
sqlCon.ConnectionString = "server=.;uid=sa;pwd=;database=SearchDB";
//定义SQL语句
string SqlStr = "select * from Discussion";
//实例化SqlDataAdapter对象
SqlDataAdapter da = new SqlDataAdapter(SqlStr, sqlCon);
//实例化数据集DataSet
DataSet ds = new DataSet();
da.Fill(ds, "Discussion");
//绑定DataList控件
GridView1.DataSource = ds;//设置数据源,用于填充控件中的项的值列表
GridView1.DataBind();//将控件及其所有子控件绑定到指定的数据源
}
获取你查询的值!
使用select 字段 from 表查询表中的数据
然后重新填充 GridView
t240034137 2009-09-04
  • 打赏
  • 举报
回复



SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings("dbconnectionstring"));
SqlCommand cmd = new SqlCommand("select * from tb where 商品名称=Keyword.Text", conn);
conn.Open();


我想把查询出来信息显示在gridview
DataSet 怎么写呀
yqyqyoyo 2009-09-04
  • 打赏
  • 举报
回复
Page_Load 事件中, gridview.datasource=数据源
gridview.databind();

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧