Automating the double click required to resize a datagrids columns
-
Having got fed up of having to continually double click my way through each of the columns in my DataGrid component, is there anyway of either.. 1) Automatically getting the datagrid to resize its columns to the data or... 2) Automating this double click at the join of all the columns in a datagrid Cheers
Give me strength, give me caffeine
-
Having got fed up of having to continually double click my way through each of the columns in my DataGrid component, is there anyway of either.. 1) Automatically getting the datagrid to resize its columns to the data or... 2) Automating this double click at the join of all the columns in a datagrid Cheers
Give me strength, give me caffeine
Hey Keith, There is a private method in the DataGrid control class that you can’t access directly through coding, however, all is not lost as Reflection can assist you here. I’ve done this before and implemented however I can’t find the souce I used so here is a brain dump from what I can remember:
// You'd probably want to do this every time the data grid changes it's data // so implement the DataSourceChanged event... private void MyDataGrid_DataSourceChanged(object sender, System.EventArgs e) { try { // First lets get the assembly's type reference... Type assemblyType = MyDataGrid.GetType(); // The DataGrid has a method called ColAutoResize (declared as private and no-one knows why) // We can invoke this by getting a reference to it in the MethodInfo class. MethodInfo methodInfo = assemblyType.GetMethod("ColAutoResize", BindingFlags.NonPublic); // Iterate through your columns... for (int i = MyDataGrid.FirstVisibleColumn; (i < MyDataGrid.VisibleColumnCount); i++) { // Invoke the ColAutoResize method, the method expects a single integer // column index as the parameter, so we’ll pass I as an argument in the // object[] array. methodInfo.Invoke(MyDataGrid, new object[] { i }); } } catch (Exception ex) { // Something went horribly wrong, examine the exception and deal // with it accordingly or just simply ignore it, this shouldn’t // happen anyhow... } }
I hope this gets you furthur in your project, happy reflectioning! :)
Fernando Mendes Senior .NET Developer, Architect
-
Having got fed up of having to continually double click my way through each of the columns in my DataGrid component, is there anyway of either.. 1) Automatically getting the datagrid to resize its columns to the data or... 2) Automating this double click at the join of all the columns in a datagrid Cheers
Give me strength, give me caffeine