`
happmaoo
  • 浏览: 4342190 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

关于DataGrid的动态排序问题

阅读更多
function StorePage() { d=document; t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():''); void(keyit=window.open('http://www.365key.com/storeit.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'keyit','scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes')); keyit.focus(); }

我有这么一个设计界面的设计习惯:我喜欢把数据库的”Comment”字段从任何显示数据库列表的控件中过滤掉,然后通过跟踪用户鼠标的单击,动态地将它的内容显示在旁边另一块我觉得更适合显示它的地方,比如说一个EditBox(Multiline = true)。以前这个方法动作得很好,因为我没有采用DataGrid,而是用的像ListBox一样的控件,但DataGrid强大的功能反而给我出了一道难题。因为在DataGrid的界面上用户可以通过点击ColumnHeader来改变数据显示的排列顺序。而在我的代码里头,刚开始只能通过重写MouseDown的事件,利用DataGrid的当前行的行号或者叫索引来安排从本地DataView中读取数据并显示在其它地方。后来我发现,当用户点击ColumnHeader以后数据在DataGrid中的排列顺序改变了,在旁边显示出来的信息,比如Comment,就对不上号了。

后来我发现,每次点击GataGridColumnHeader后,它上面的小箭头都会改变方向,但每个ColumnHeader的箭头方向是独立的。说它们是独立的是因为每列的箭头都只有被点击后才会改变方向,如果点击的是另一个列,那么这将对本列没有影响。举个列子,有如下两个列,从箭头的方向来看,数据是按Id列升序排列的:

Id Name

现在用户点击了第一个ColumnHeader,结果将变成下面这种情况:

Id Name

这个时候如果用户继续点击第一列,那么它的箭头方向将变为向上,而点击后者,其箭头方向会变成设下,而不是向上,因为它默认的就是升序排列。

如果有一个方法让这些箭头都统一起来,我就可以通过动态地改变DataViewSort属性来对应新的排列顺序,因为通过捕获DataGridMouseDown事件我可以获取被点击的ColumnHeaderHeaderText

那么怎样使这些箭头都统一起来呢?

用一个布尔变量!

是的,就这个简单,但想到可不容易。于是我在当前窗体类中设置了一个布尔变量,取名为bSortDESC并将之初始为了false,因为这是默认的。那么接下来,当用户点击“列头”时,我就在DataGridMouseDown事件中通过代码得到当前列的列名。将布尔变量作一个逆变换后判断它的值,以决定将DataViewSort属性改成什么。这样一来,后台的数据视图就会动态地跟随前台的变量而改变顺序。于是乎,显示在旁边的其它数据也就可以对上号了。

下面用一段代码再作一翻解释:

//在Form1类的定义中声明一个受保护的布尔变量

private bool bSortDESC = false;

//在Form1_Load中初始化

SqlCommand cmd = Global.cnn.CreateCommand();
cmd.CommandText = "spGetEmpInfo";
cmd.CommandType = CommandType.StoredProcedure;
this.da = new SqlDataAdapter(cmd);
this.ds = new DataSet();
this.dv = new DataView();
this.da.FillSchema(this.ds, SchemaType.Source);
this.da.Fill(this.ds, "tblEmpInfo");
this.dv.Table = this.ds.Tables["tblEmpInfo"];
this.dv.Sort = "Id";

this.dg.DataSource = this.dv
DataGridTableStyle ts = new DataGridTableStyle();
ts.MappingName ="tblEmpInfo";

this.dg.TableStyles.Add(ts);
this.dg.TableStyles[0].GridColumnStyles["Comment"].Width = 0;

//下面是DataGrid的MouseDown事件

private void dgDoc_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
DataGrid tmpDg = (DataGrid)sender;
DataGrid.HitTestInfo hti = tmpDg.HitTest(e.X, e.Y);

switch(hti.Type)
{
case DataGrid.HitTestType.ColumnHeader:
{
try
{
int nColumnIndex = hti.Column;
string strColumnHeader = this.dg.TableStyles[0].GridColumnStyles[nColumnIndex].HeaderText;

this.bSortDESC = !this.bSortDESC;
if (this.bSortDESC)
{
this.dv.Sort = strColumnHeader + " ASC";
}
else
{
this.dv.Sort = strColumnHeader + " DESC";
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}break;
}
}

//如果您选择将某些字段在DataGrid之外显示并将它们与数据源绑定在一起时,请加入以下的CurrentCellChanged事件处理代码

private void dgDoc_CurrentCellChanged(object sender, System.EventArgs e)
{
// now the dg row index can be treated as the row index of the data in
// dv since dv has been re-sorted
BindingContext[this.].Position = this.dg.CurrentRowIndex;
this.UpdateScreen();
}

//最后是更新界面的方法(rtb代表RichTextBox)

private void UpdateScreen()
{
this.rtbComment.Clear();
this.rtbComment.Text = this.dv[BindingContext[this.dv].Position]["Comment"].ToString();

这样,当你点击DataGrid的ColumnHeader时,上面箭头的方向总是一上一下,即使你点击在不同的列上.而至于后台的视图,已经动态地重新排列了.

这是我自己想出来的方法,可能有不足之处,如果谁有更好的方法,希望能和大家分享d:)

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics