回复 7楼victorysoft的帖子
关于 form1 的问题需要在设置数据源之前重置 表单,修改后如下:
- namespace 数据源绑定和分组问题Demo
- {
- public partial class Form1 : Form
- {
- DataTable dt = new DataTable();
- public Form1()
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- dt.Columns.Add("名称");
- dt.Columns.Add("价格");
- dt.Rows.Add("小米", "1000");
- dt.Rows.Add("小米", "1000");
- dt.Rows.Add("酷派", "800");
- dt.Rows.Add("中兴", "900");
- //设置邦定从第二列开始(第一列预留)
- this.fpSpread1_Sheet1.BindDataColumn(1, "名称");
- this.fpSpread1_Sheet1.BindDataColumn(2, "价格");
- this.fpSpread1_Sheet1.AutoGenerateColumns = false;
- QueryData();
- SettingForDefaultGroupFooter();
- }
- private void QueryData()
- {
- #region 模拟创建新的数据源
- DataTable dt1 = new DataTable();
- dt1 = dt.Clone();
- DataRow[] drs = null;
- if (this.textBox1.Text != "")
- {
- drs = dt.Select("名称 like '%" + this.textBox1.Text + "%'");
- }
- else
- {
- drs = dt.Select();
- }
- foreach (DataRow dr in drs)
- {
- dt1.Rows.Add(dr.ItemArray);
- }
- #endregion
- //绑定数据源
- this.fpSpread1.Sheets[0].Reset();
- this.fpSpread1.Sheets[0].DataSource = dt1;
- SettingForDefaultGroupFooter();
- }
- //设置分组
- private void SettingForDefaultGroupFooter()
- {
- GroupDataModel gdm = new GroupDataModel(this.fpSpread1_Sheet1.Models.Data);
- SortInfo[] siList = new SortInfo[] { new SortInfo(1, true) };
- gdm.Group(siList);
- this.fpSpread1_Sheet1.Models.Data = gdm;
- }
- private void button1_Click(object sender, EventArgs e)
- {
- QueryData();
- }
- }
- }
复制代码 |