Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with error in code found in the net (DataGrid)

Status
Not open for further replies.

vbSun

Programmer
Dec 9, 2002
504
US
Code:
public void AutoSizeGrid(DataGrid dGrid) 
 		{ 
 			// DataGrid should be bound to a DataTable for this part to 
 			// work. 
 
			int numRows = ((DataTable)dGrid.DataSource).Rows.Count; 
 			Graphics g = Graphics.FromHwnd(dGrid.Handle); 
 			StringFormat sf = new StringFormat(StringFormat.GenericTypographic); 
 			SizeF size; 
  
			// Since DataGridRows[] is not exposed directly by the DataGrid 
 			// we use reflection to hack internally to it.. There is actually 
 			// a method get_DataGridRows that returns the collection of rows 
 			// that is what we are doing here, and casting it to a System.Array 
 
			MethodInfo mi = dGrid.GetType().GetMethod("get_DataGridRows", 
 				BindingFlags.FlattenHierarchy | BindingFlags.IgnoreCase | BindingFlags.Instance 
 				| BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static); 
 			System.Array dgra = (System.Array)mi.Invoke(dGrid,null); 
  
			// Convert this to an ArrayList, little bit easier to deal with 
 			// that way, plus we can strip out the newrow row. 
 
			ArrayList DataGridRows = new ArrayList(); 
 			foreach (object dgrr in dgra) 
 			{ 
 				if (dgrr.ToString().EndsWith("DataGridRelationshipRow")==true) 
 					DataGridRows.Add(dgrr); 
 			} 
 
			// Now loop through all the rows in the grid 
 			for (int i = 0; i < numRows; ++i) 
 			{ 
 				// Here we are telling it that the column width is set to 
 				// 400.. so size will contain the Height it needs to be. 
 				size = g.MeasureString(dGrid[i,1].ToString(),dGrid.Font,400,sf); 
 				int h = Convert.ToInt32(size.Height); 
 				// Little extra cellpadding space 
 				h = h + 8; 
 
				// Now we pick that row out of the DataGridRows[] Array 
 				// that we have and set it's Height property to what we 
 				// think it should be. 
 
				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.. 
 			} 
 			g.Dispose(); 
 		}

I am using the above code in my C# windows application. But am not getting it to work. One error message says "Specified cast is not valid." in line "int numRows = ((DataTable)dGrid.DataSource).Rows.Count; "

Is there anything that I am doing incorrectly? (Well, i thought copy/paste programming works!)

------------------------------------------
The faulty interface lies between the chair and the keyboard.
 
Is the datagrid bound to a datatable when this error is thrown? Can you check it is in Debug?

Rhys
The use of COBOL cripples the mind; its teaching should, therefore, be regarded as a criminal offense Edsgar Dijkstra

Church of the Flying Spaghetti Monster
 
yes it is Rhys666.. but I guess I had fixed the code.. as usual, (programmer's lingo) "suddenly it started working!"

------------------------------------------
The faulty interface lies between the chair and the keyboard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top