如何在DataGrid表格里实现数据过滤显示?

soft_fly 2003-10-16 03:49:01
问各位大哥,在C#里如何用Sql语句查询数据库中的数据,实现在DataGrid表格里显示?
谢谢!
...全文
85 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
amoxicillin1030 2003-10-20
  • 打赏
  • 举报
回复
应该可以用的,只是我不知道怎么用罢了
soft_fly 2003-10-20
  • 打赏
  • 举报
回复
呵,说是这样说,但,我总认为,是Visual的,就应该不要在写那些没有必要的了,就像Delphi那样,多好!
amoxicillin1030 2003-10-20
  • 打赏
  • 举报
回复
一般都是自己写的,又不麻烦
而且这样升级维护起来也比较方便
别人读你的程序的时候也很清晰
soft_fly 2003-10-20
  • 打赏
  • 举报
回复
啊,不会吧,如果这样的话,那能叫Visual吗?跟Asp此类的就没什么区别了?
amoxicillin1030 2003-10-20
  • 打赏
  • 举报
回复
建议你自己写,不要用控件
我用的时候出现过同样的问题,不知道怎么解决,最后还是自己写啦
soft_fly 2003-10-20
  • 打赏
  • 举报
回复
像这些“SqlDataAdapter,DataSet”我是直接用控件的,连接方式也是在里面设的
我的语句是这样的
sqlCommand1.CommandText="Select * From T_Reg Where regname='"+txtbox.Text+"'";
DataGrid1.DataSource=dataSet11.Tables["T_Reg"].DefaultView;
DataGrid1.DataBind();
这样子,不会,网格里的数据还是没变化?
feigehao 2003-10-19
  • 打赏
  • 举报
回复
donger2000(东东) .你为什么写些这样的没有用的东西呢。这些初始化的有什么用呀
donger2000 2003-10-17
  • 打赏
  • 举报
回复
楼上说得对,在SQL语句中,你可以用where/having 子句过滤记录,在C#中,你还可以用DataView.RowFilter过滤记录
herofyf 2003-10-17
  • 打赏
  • 举报
回复
方法多多!
还有用表过滤的方法!我想你还是先看一下帮助啊!
amoxicillin1030 2003-10-17
  • 打赏
  • 举报
回复
this.dataGrid1.DataBindings.Clear();
string constr=@"uid=sa;pwd=kk;server=wlj;database=project";
string sql=" SELECT ID,Name FROM Project WHERE Name = WZ;
\\在WHERE后设置你的条件
SqlConnection conn=new SqlConnection(constr);
da=new SqlDataAdapter(sql,conn);
SqlCommandBuilder cb=new SqlCommandBuilder(da);
ds=new DataSet();
da.Fill(ds,"Project");
dataGrid1.DataSource=ds.Tables["Project"].DefaultView;
soft_fly 2003-10-16
  • 打赏
  • 举报
回复
楼上的大哥,你说的没错,我的意思就是这样,请指教,谢谢!
amoxicillin1030 2003-10-16
  • 打赏
  • 举报
回复
你得意思我不太明白
是要按某个条件查询然后将符合条件得数据显示在DataGrid内吗
soft_fly 2003-10-16
  • 打赏
  • 举报
回复
小弟刚学,不太明白,能不能写清楚点?
谢谢!
孟子E章 2003-10-16
  • 打赏
  • 举报
回复
http://xml.sz.luohuedu.net/xml/ShowDetail.asp?id=12912741-070C-41DE-8D08-50C8DDA1F864
donger2000 2003-10-16
  • 打赏
  • 举报
回复
微软的表格控件示例:

namespace Microsoft.Samples.WinForms.Cs.Grid {
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

using System.Data;
using System.Data.SqlClient;

public class Grid : System.Windows.Forms.Form {
private System.ComponentModel.Container components;
private System.Windows.Forms.StatusBar statusBar1;
private Microsoft.Samples.WinForms.Cs.Grid.Data.CustomersDataSet customersDataSet1;
private System.Windows.Forms.Button buttonLoad;
private System.Windows.Forms.DataGrid dataGrid1;

public Grid() {
// Required by the Windows Forms Designer
InitializeComponent();

if ( dataGrid1.TableStyles.Count == 0 ) {
dataGrid1.TableStyles.Add(new DataGridTableStyle());
}
this.dataGrid1.TableStyles[0].PreferredRowHeight = 16;
this.dataGrid1.TableStyles[0].AlternatingBackColor = System.Drawing.Color.WhiteSmoke;
}

//Handle the Load Button Click
//Load the Customers, Orders and OrderDetails Tables and display in the Grid
private void buttonLoad_Click(object sender, System.EventArgs e) {
Cursor currentCursor = Cursor.Current;
SqlConnection con = new SqlConnection("server=(local);Integrated Security=SSPI;database=northwind");
try {
Cursor.Current = Cursors.WaitCursor;

//Fill the DataSet
SqlDataAdapter cmdCustomers = new SqlDataAdapter("Select * from Customers", con);
SqlDataAdapter cmdOrders = new SqlDataAdapter("Select * from Orders", con);
SqlDataAdapter cmdOrderDetails = new SqlDataAdapter("Select * from [Order Details]", con);
customersDataSet1.Clear();
statusBar1.Text ="Loading Customers...";
cmdCustomers.Fill(customersDataSet1, "Customers");
statusBar1.Text ="Loading Orders...";
cmdOrders.Fill(customersDataSet1, "Orders");
statusBar1.Text ="Loading Order Details...";
cmdOrderDetails.Fill(customersDataSet1, "Order_Details");
statusBar1.Text ="Updating Grid...";
} finally {
statusBar1.Text ="Done";
Cursor.Current = currentCursor;
con.Close();
}
}


protected override void Dispose(bool disposing)
{
if (disposing) {
if (components != null) {
components.Dispose();
}
}
base.Dispose(disposing);
}

private void InitializeComponent() {
this.components = new System.ComponentModel.Container();
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.statusBar1 = new System.Windows.Forms.StatusBar();
this.customersDataSet1 = new Microsoft.Samples.WinForms.Cs.Grid.Data.CustomersDataSet();
this.buttonLoad = new System.Windows.Forms.Button();

dataGrid1.BeginInit();
this.dataGrid1.Text = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(584, 336);
this.dataGrid1.DataSource = customersDataSet1;
this.dataGrid1.DataMember = "Customers";
this.dataGrid1.ForeColor = System.Drawing.Color.Navy;
this.dataGrid1.TabIndex = 0;
this.dataGrid1.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
this.dataGrid1.Location = new System.Drawing.Point(8, 8);
this.dataGrid1.BackColor = System.Drawing.Color.Gainsboro;

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.Text = "Customer Details";
this.AcceptButton = buttonLoad;
this.ClientSize = new System.Drawing.Size(600, 413);

this.statusBar1.BackColor = System.Drawing.SystemColors.Control;
this.statusBar1.Location = new System.Drawing.Point(0, 397);
this.statusBar1.Size = new System.Drawing.Size(600, 16);
this.statusBar1.TabIndex = 2;
this.statusBar1.Text = "Click on Load";

this.customersDataSet1.DataSetName = "CustomersDataSet";

this.buttonLoad.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
this.buttonLoad.Click += new System.EventHandler(buttonLoad_Click);
this.buttonLoad.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.buttonLoad.Location = new System.Drawing.Point(480, 352);
this.buttonLoad.Size = new System.Drawing.Size(112, 32);
this.buttonLoad.TabIndex = 1;
this.buttonLoad.Text = "&Load";
this.Controls.AddRange(new System.Windows.Forms.Control[] {statusBar1,
buttonLoad,
dataGrid1});
this.dataGrid1.EndInit();
}

[STAThread]
public static void Main(string[] args) {
Application.Run(new Grid());
}

}
}







acewang 2003-10-16
  • 打赏
  • 举报
回复
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<html>

<script language="VB" runat="server">

Sub Page_Load(Sender As Object, E As EventArgs)

Dim DS As DataSet
Dim MyConnection As SqlConnection
Dim MyCommand As SqlDataAdapter

MyConnection = New SqlConnection("server=(local)\NetSDK;database=pubs;Trusted_Connection=yes")
MyCommand = New SqlDataAdapter("select * from Authors", MyConnection)

DS = new DataSet()
MyCommand.Fill(ds, "作者")

MyDataGrid.DataSource=ds.Tables("作者").DefaultView
MyDataGrid.DataBind()
End Sub

</script>

<body>

<h3><font face="宋体">DataGrid 控件的简单选择</font></h3>

<ASP:DataGrid id="MyDataGrid" runat="server"
Width="700"
BackColor="#ccccff"
BorderColor="black"
ShowFooter="false"
CellPadding=3
CellSpacing="0"
Font-Name="宋体"
Font-Size="8pt"
HeaderStyle-BackColor="#aaaadd"
EnableViewState="false"
/>

</body>
</html>
amitabha 2003-10-16
  • 打赏
  • 举报
回复
ado.net,慢慢看
名称 类型 描述 默认值 columns(列) array(数组) 数据表格列配置对象,查看列属性以获取更多细节。 null frozenColumns(固定列) array(数组) 跟列属性一样,但是这些列固定在左边,不会滚动。 null fitColumns(自适应列宽) boolean(布尔型) 设置为true将自动使列适应表格宽度以防止出现水平滚动。 false striped(显示条纹) boolean(布尔型) 设置为true将交替显示行背景。 false method(方法) string(字符串) 请求远程数据的方法类型。 post nowrap(截取) boolean(布尔型) 设置为true,当数据长度超出列宽时将会自动截取。 true idField(id字段) string(字符串) 表明该列是一个唯一列。 null url(超链接) string(字符串) 一个用以从远程站点请求数据的超链接地址。 null loadMsg(载入时信息) string(字符串) 当从远程站点载入数据时,显示的一条快捷信息。 Processing, please wait … pagination(分页) boolean(布尔型) 设置true将在数据表格底部显示分页工具栏。 false rownumbers(行数) boolean(布尔型) 设置为true将显示行数。 false singleSelect(单选模式) boolean(布尔型) 设置为true将只允许选择一行。 false pageNumber(当前页码) number(数字) 当设置分页属性时,初始化分页码。 1 pageSize(每页记录数) number(数字) 当设置分页属性时,初始化每页记录数。 10 pageList(可选择的每页记录数) array 当设置分页属性时,初始化每页记录数列表。 [10,20,30,40,50] queryParams(查询参数) object(对象) 当请求远程数据时,发送的额外参数。 {} sortName(默认排序) string(字符串) 当数据表格初始化时以哪一列来排序。 null sortOrder(排序顺序) string(字符串) 定义排序顺序,可以是'asc'或者'desc'(正序或者倒序)。 asc remoteSort(远程排序) boolean(布尔型) 定义是否通过远程服务器对数据排序。 true showFooter(显示行底) boolean(布尔型) 定义是否显示行底(如果是做统计表格,这可以显示总计等)。 false rowStyler(行样式) function(函数) 返回样式,如:'background:red',function有2个参数: index:行索引,从0开始. row:对应于该行记录的对象。 loadFilter(载入过滤器) function(函数) 返回用以显示的已过滤数据,function有一个参数'data'表示原始数据,你可以将原始数据改变为规范的数据格式,该函数必须返回包含 'total'和'rows'属性的标准数据对象。 editors(编辑模式) object(对象) 定义当编辑一行时的编辑模式。 predefined editors view(视图) object(对象) 定义数据表格的视图。 default view

110,500

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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