原文出处:http://www.syncfusion.com/FAQ/WinForms/FAQ_c44c.asp#q874q
1。我怎样用程序设置DataGrid其中某行的rowheight (原文http://www.syncfusion.com/FAQ/WinForms/FAQ_c44c.asp#q1075q)
可以通过映射来访问DataGrid的内部没公共申明的(public)Row对象,此方法在Matthew Benedict 的一次私人交流上提到。
这个包含vb和c#的工程文件示意了这个方法的实现,例子提供了通过DataGrid创建rowHeight对象的一个类,在创建了这个对象后,可以用此对象的索引器来设置或提取一行的高度。
2。我的DataGrid如何自动设置rowheights
Matthew Benedict的一个建议的解决办法:通过映射访问保护的DataGridRows集合来设置行高。
public void AutoSizeGrid()
|
// work. 这里这部分DataGrid必须绑定到DataTable
|
int numRows = ((DataTable)gridTasks.DataSource).Rows.Count;
|
Graphics g = Graphics.FromHwnd(gridTasks.Handle);
|
StringFormat sf = new StringFormat(StringFormat.GenericTypographic);
|
// 因为DataGrid中DataGridRows[]没有直接暴露
|
// 我们需要用到映射机制.. 下面的get_DataGridRows方法返回我们正需要的行集合 并投射到System.Array
|
MethodInfo mi = gridTasks.GetType().GetMethod("get_DataGridRows",
|
BindingFlags.FlattenHierarchy | BindingFlags.IgnoreCase | BindingFlags.Instance
|
| BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
|
System.Array dgra = (System.Array)mi.Invoke(gridTasks,null);
|
// 转换为ArrayList,一种更容易处理的方式,追加到我们剥离的新行。
|
ArrayList DataGridRows = new ArrayList();
|
foreach (object dgrr in dgra)
|
if (dgrr.ToString().EndsWith("DataGridRelationshipRow")==true)
|
for (int i = 0; i < numRows; ++i)
|
// 这里我们讲列的宽度设置为400,此大小将包含所需的高度 |
size = g.MeasureString(gridTasks[i,1].ToString(),gridTasks.Font,400,sf);
|
int h = Convert.ToInt32(size.Height);
|
// 现在我们将已经设置成所需高度属性的行从DataGridRows[]数组中取出 |
PropertyInfo pi = DataGridRows[i].GetType().GetProperty("Height");
|
pi.SetValue(DataGridRows[i],h,null);
|
// I have read here that after you set the Height in this manner that you should
|
// Call the DataGrid Invalidate() method, but I haven't seen any prob with not calling it..
|
3。在DataGrid里怎样防止一列排序
派生DataGrid并覆盖(重写)OnMouseDowm。重写HitText ,如果单击在不希望排序的那列列首,不调用基类的处理事件。下面是排序所有列除了第二列的代码
public class MyDataGrid : DataGrid
|
protected override void OnMouseDown(MouseEventArgs e)
|
Point pt = new Point(e.X, e.Y);
|
DataGrid.HitTestInfo hti = this.HitTest(pt);
|
if(hti.Type == HitTestType.ColumnHeader && hti.Column == 1)
|
return; //don't call baseclass
|
Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs)
|
Dim pt As New Point(e.X, e.Y)
|
Dim hti As DataGrid.HitTestInfo = Me.HitTest(pt)
|
If hti.Type = HitTestType.ColumnHeader AndAlso hti.Column = 1 Then
|
Return 'don't call baseclass
|
4。如何程序化的选择一行
代码如下
this.dataGrid1.Select(1);
|
5。