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

soft_fly 2003-10-16 03:49:01
问各位大哥,在C#里如何用Sql语句查询数据库中的数据,实现在DataGrid表格里显示?
谢谢!
...全文
88 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,慢慢看

110,534

社区成员

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

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

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