原帖子地址:http://topic.csdn.net/u/20090526/15/9479e39f-daf8-48e2-94c1-2761eb469c69.html网页:http://msdn.microsoft.com/zh-cn/library/aa480727(en-us).aspx
Demo下载:http://download.microsoft.com/download/4/2/1/4215444a-ed19-45fe-84a9-2eb0b7b9dcbb/DataGridViewAutoFilter.zip 我的问题是:
Demo里面的例子是用控件绑定数据源,
我想修改为,用代码来绑定数据源,如下代码:
private void DesignerSetupForm_Load(object sender, EventArgs e)
{
//这个是我自己项目里面的Load事件代码:
strSql = "select top 20 订单编号,贸易方式,价格条款 from dd订单";
SqlDataAdapter dap = new SqlDataAdapter(strSql, clsSql.cn);
DataSet ds = new DataSet();
dap.Fill(ds, "dd订单");
dataGridView1.RowCount = ds.Tables[0].Rows.Count + 1;//报错:不能在数据绑定的DataGridView控件上设置RowCount属性
for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
{
this.dataGridView1.Rows[j].Cells[0].Value = ds.Tables[0].Rows[j]["订单编号"].ToString();
this.dataGridView1.Rows[j].Cells[1].Value = ds.Tables[0].Rows[j]["贸易方式"].ToString();
this.dataGridView1.Rows[j].Cells[2].Value = ds.Tables[0].Rows[j]["价格条款"].ToString();
}
ds.Dispose();
}
我没看懂Demo里面的例子,不会用;
谁能帮我分析下这个Demo,或者写个用代码绑定数据的Demo,贴出来看下;我实在是想不到其他的办法了
只要解决这个难题,就算回到贫农时代,我也乐意了;
Demo下载:http://download.microsoft.com/download/4/2/1/4215444a-ed19-45fe-84a9-2eb0b7b9dcbb/DataGridViewAutoFilter.zip 我的问题是:
Demo里面的例子是用控件绑定数据源,
我想修改为,用代码来绑定数据源,如下代码:
private void DesignerSetupForm_Load(object sender, EventArgs e)
{
//这个是我自己项目里面的Load事件代码:
strSql = "select top 20 订单编号,贸易方式,价格条款 from dd订单";
SqlDataAdapter dap = new SqlDataAdapter(strSql, clsSql.cn);
DataSet ds = new DataSet();
dap.Fill(ds, "dd订单");
dataGridView1.RowCount = ds.Tables[0].Rows.Count + 1;//报错:不能在数据绑定的DataGridView控件上设置RowCount属性
for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
{
this.dataGridView1.Rows[j].Cells[0].Value = ds.Tables[0].Rows[j]["订单编号"].ToString();
this.dataGridView1.Rows[j].Cells[1].Value = ds.Tables[0].Rows[j]["贸易方式"].ToString();
this.dataGridView1.Rows[j].Cells[2].Value = ds.Tables[0].Rows[j]["价格条款"].ToString();
}
ds.Dispose();
}
我没看懂Demo里面的例子,不会用;
谁能帮我分析下这个Demo,或者写个用代码绑定数据的Demo,贴出来看下;我实在是想不到其他的办法了
只要解决这个难题,就算回到贫农时代,我也乐意了;
哪里设置"自动生成为fals"? 没找到?
temp++;
试了那例子 感觉关键是怎么给NewDataSet赋值
指定dataset再bind就可以了吧
private void DesignerSetupForm_Load(object sender, EventArgs e)
{
//这个是我自己项目里面的Load事件代码:
strSql = "select top 20 订单编号,贸易方式,价格条款 from dd订单";
SqlDataAdapter dap = new SqlDataAdapter(strSql, clsSql.cn);
DataSet ds = new DataSet();
dap.Fill(ds, "dd订单");
BindingSource dataSource = new BindingSource(ds.Tables[0], null);
dataGridView1.DataSource = dataSource;
//ds.Dispose();
} private void dataGridView1_BindingContextChanged(object sender, EventArgs e)
{
// Continue only if the data source has been set.
if (dataGridView1.DataSource == null)
{
return;
} // Add the AutoFilter header cell to each column.
foreach (DataGridViewColumn col in dataGridView1.Columns)
{
col.HeaderCell = new
DataGridViewAutoFilterColumnHeaderCell(col.HeaderCell);
} // Resize the columns to fit their contents.
dataGridView1.AutoResizeColumns();
}注:下面这个是核心事件,一定要加
之前一直忽略这个Demo,现在好好研究下,可能是解开难题的关键,谢谢提醒;每天+100分,直到问题解决,或者可以分变为乞丐;
弄懂这个,对我很重要,请求各位的帮助,SOS...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace ProgrammaticSetupDemo
{
public class ProgrammaticSetupForm : Form
{
DataGridView dataGridView1 = new DataGridView();
StatusStrip statusStrip1 = new StatusStrip();
ToolStripStatusLabel filterStatusLabel = new ToolStripStatusLabel();
ToolStripStatusLabel showAllLabel = new ToolStripStatusLabel("Show &All"); // Initializes the form.
public ProgrammaticSetupForm()
{
dataGridView1.Name = "dataGridView1";
dataGridView1.Dock = DockStyle.Fill;
dataGridView1.BindingContextChanged += new EventHandler(dataGridView1_BindingContextChanged);
dataGridView1.KeyDown += new KeyEventHandler(dataGridView1_KeyDown);
dataGridView1.DataBindingComplete +=new DataGridViewBindingCompleteEventHandler(dataGridView1_DataBindingComplete); showAllLabel.Visible = false;
showAllLabel.IsLink = true;
showAllLabel.LinkBehavior = LinkBehavior.HoverUnderline;
showAllLabel.Click += new EventHandler(showAllLabel_Click); statusStrip1.Cursor = Cursors.Default;
statusStrip1.Items.AddRange(new ToolStripItem[] { filterStatusLabel, showAllLabel }); this.Text = "DataGridView AutoFilter Demo (C# Programmatic Setup)";
this.Width *= 3;
this.Height *= 2;
this.Controls.AddRange(new Control[] {dataGridView1, statusStrip1 });
} // Initializes the data source.
protected override void OnLoad(EventArgs e)
{
DataTable data = new DataTable();
data.ReadXmlSchema(@"..\..\..\..\..\TestData.xsd");
data.ReadXml(@"..\..\..\..\..\TestData.xml");
BindingSource dataSource = new BindingSource(data, null);
dataGridView1.DataSource = dataSource;
base.OnLoad(e);
} // Configures the autogenerated columns, replacing their header
// cells with AutoFilter header cells.
private void dataGridView1_BindingContextChanged(object sender, EventArgs e)
{
// Continue only if the data source has been set.
if (dataGridView1.DataSource == null)
{
return;
} // Add the AutoFilter header cell to each column.
foreach (DataGridViewColumn col in dataGridView1.Columns)
{
col.HeaderCell = new
DataGridViewAutoFilterColumnHeaderCell(col.HeaderCell);
} // Format the OrderTotal column as currency.
dataGridView1.Columns["OrderTotal"].DefaultCellStyle.Format = "c";
// Resize the columns to fit their contents.
dataGridView1.AutoResizeColumns();
} // Displays the drop-down list when the user presses
// ALT+DOWN ARROW or ALT+UP ARROW.
private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Alt && (e.KeyCode == Keys.Down || e.KeyCode == Keys.Up))
{
DataGridViewAutoFilterColumnHeaderCell filterCell =dataGridView1.CurrentCell.OwningColumn.HeaderCell as DataGridViewAutoFilterColumnHeaderCell;
if (filterCell != null)
{
filterCell.ShowDropDownList();
e.Handled = true;
}
}
}
}
}这些是ProgrammaticSetupDemo的关键代码;
OnLoad事件,里面使用xml路径绑定数据源的,
怎么修改为用代码绑定?(如1楼的Load事件)
我测试的项目你可以在这里下载http://www.brsbox.com/filebox/down/fc/4e42c0562bf8f757d79130862befbae4你的代码稍微改下应该可以了,BindingContextChanged事件可以不用实现. dap.Fill(ds, "dd订单");
BindingSource dataSource = new BindingSource();
dataSource.DataMember = "dd订单";
dataSource.DataSource = ds; dataGridView1.DataSource = dataSource;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//
using System.Threading;
using System.IO;
using SMGe;
using System.Data.SqlClient;namespace SMGe
{
public partial class Frm删 : Form
{
private string strSql = ""; public Frm删()
{
InitializeComponent();
} private void Frm删_Load(object sender, EventArgs e)
{
strSql = "select * from dd订单";
DataSet ds = clsSql.GetDs(strSql);
BindingSource dataSource = new BindingSource(ds.Tables[0], null);
dataGridView1.DataSource = dataSource;
} private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Alt && (e.KeyCode == Keys.Down || e.KeyCode == Keys.Up))
{
DataGridViewAutoFilterColumnHeaderCell filterCell = dataGridView1.CurrentCell.OwningColumn.HeaderCell as DataGridViewAutoFilterColumnHeaderCell;
if (filterCell != null)
{
filterCell.ShowDropDownList();
e.Handled = true;
}
}
} private void dataGridView1_BindingContextChanged(object sender, EventArgs e)
{
if (dataGridView1.DataSource == null)
{
return;
} foreach (DataGridViewColumn col in dataGridView1.Columns)
{
col.HeaderCell = new DataGridViewAutoFilterColumnHeaderCell(col.HeaderCell);
}
}
}
}
我也想要啊 ,可是 我是学习VB。NET 的呢
修改成
dataGridView1.RowCount = ds.Tables[0].Rows.Count
在设置allowusertoaddrows=false