using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;namespace datagrid
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.Button button1; private string connection="workstation id =localhost; Integrated Security=SSPI;database=jxcbook";
                                                             

private  string sendTableName="商品清单";
private string sendSQL="select * from 商品清单";
private DataSet ds=new DataSet();
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
SqlConnection myConnection=new SqlConnection(connection);
SqlDataAdapter da=new SqlDataAdapter(sendSQL,myConnection);
this.ds.Clear();
da.Fill(ds,sendTableName);
this.dataGrid1.DataSource=ds.Tables[0]; //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null) 
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.button1 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
// 
// dataGrid1
// 
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(8, 80);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(280, 144);
this.dataGrid1.TabIndex = 0;
// 
// button1
// 
this.button1.Location = new System.Drawing.Point(56, 32);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(64, 24);
this.button1.TabIndex = 1;
this.button1.Text = "查询";
this.button1.Click += new System.EventHandler(this.button1_Click);
// 
// Form1
// 
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.button1);
this.Controls.Add(this.dataGrid1);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main() 
{
Application.Run(new Form1());
} private void btn_Search_Click(object sender, System.EventArgs e)
{
//显示当查询货号为001时的商品信息,代码如何写,急盼

}
}
}

解决方案 »

  1.   

    DataView myDataView = new DataView(myDataSet.Tables["yourTableName"]);// Filter the dataview to only show customers with the CustomerID of ALFKI
    myDataView.RowFilter = "货号='001'";//Add your filter here
      

  2.   

    我的意思是用DataGrid控件啊,我认为是一个SQL select语句,然后就是怎么把结果反映在DataGrid中了。怎么写代码啊?急盼!~~~~
      

  3.   

    你不是已经想DataSet中进行填充了吗,然后用
    DataView dv = new DataView(ds.Tables["yourTableName"]);// Filter the dataview to only show customers with the CustomerID of ALFKI
    dv.RowFilter = "货号='001'";//Add your filter here
    dv.RowStateFilter = DataViewRowState.ModifiedCurrent;yourDataGrid.DataSource = dv;
      

  4.   

    to:Knight
    dv.RowstateFilter=DataViewRowState.ModifiedCurrend;此句有点误。
    不过没有这句话也能实现。多谢!!以后请多多关照!!
    用SQL中的select ...from where 语句怎么实现?
      

  5.   

    TO:Knight94多谢不惜赐教,现问题已解决。谢谢!