 |
|
 |
That was clever!!
I used to do this...
textColumn.Width = textColumn.HeaderText.Length > 12 ? (textColumn.HeaderText.Length * 5) + 8 :
(textColumn.HeaderText.Length * 5) + 18;
But now...
Graphics gfx = dgrid_INDICATOR.CreateGraphics();
textColumn.Width = (int)(gfx.MeasureString(textColumn.HeaderText, dgrid_INDICATOR.Font).Width) + 5;
Thanks Tom
Smile: A curve that can set a lot of things straight!
(\ /)
(O.o)
(><)
|
|
|
|
 |
|
 |
It helped a lot.Thank you again.
Good Job...
Nilesh
|
|
|
|
 |
|
 |
ur code works wonders on a normal windows app but i have trouble when i try to use it in a smart device application for pocket pc i get an error
properties[p].Converter.ConvertToString(result);
converter doesnt exist on the pocket Pc c#.
if u could help i would appreciate it
|
|
|
|
 |
|
 |
I only have one issue and that is the overwriting of my styles. I basically had to rewrite the styles I wanted (colors etc. after resizing).
3.14159265358979323846264338327950288..........
|
|
|
|
 |
|
 |
Hi,
I've taken this and made a few tweaks to it.
Firstly, it will now work on other types of bindable object. I've tested it with DataTable, IList and IListSource.
Secondly, it will now check for pre-existing table/column styles and use them if they exist (including using the HeaderText as the initial width), otherwise it will create new ones.
public static void SizeColumnsToContent(DataGrid dataGrid, int nRowsToScan)
{
IList list = GetDataSource(dataGrid);
if (list != null)
{
string mappingName = string.Empty;
if (list is ITypedList)
mappingName = (list as ITypedList).GetListName(null);
else
mappingName = list.GetType().Name;
if (nRowsToScan == -1)
nRowsToScan = list.Count;
else
nRowsToScan = System.Math.Min(nRowsToScan, list.Count);
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(list[0]);
DataGridTableStyle tableStyle = null;
if (dataGrid.TableStyles[mappingName] == null)
{
tableStyle = new DataGridTableStyle();
tableStyle.MappingName = mappingName;
dataGrid.TableStyles.Add(tableStyle);
}
else
{
tableStyle = dataGrid.TableStyles[mappingName];
}
using (Graphics g = dataGrid.CreateGraphics())
{
for (int p=0; p<properties.Count; ++p)
{
DataGridColumnStyle colStyle = null;
if (tableStyle.GridColumnStyles[properties[p].DisplayName] == null)
{
colStyle = new DataGridTextBoxColumn();
colStyle.MappingName = properties[p].Name;
colStyle.HeaderText = properties[p].DisplayName;
tableStyle.GridColumnStyles.Add(colStyle);
}
else
{
colStyle = tableStyle.GridColumnStyles[properties[p].DisplayName];
}
if (colStyle.Width > 0)
{
SizeF maxSize = g.MeasureString(colStyle.HeaderText, dataGrid.Font);
for (int r=0; r<nRowsToScan; ++r)
{
object o = list[r];
object result = properties[p].GetValue(o);
string text = properties[p].Converter.ConvertToString(result);
SizeF size = g.MeasureString(text, dataGrid.Font);
if (size.Width > maxSize.Width)
maxSize = size;
}
colStyle.Width = (int) (maxSize.Width + 5);
}
}
}
}
}
It works a treat for me, hope it helps others.
|
|
|
|
 |
|
 |
You forgot to mention about "GetDataSource" I guess
|
|
|
|
 |
|
 |
emreo wrote: You forgot to mention about "GetDataSource" I guess
oops!
public static IList GetDataSource(DataGrid dataGrid)
{
IList list = null;
IListSource listSource = dataGrid.DataSource as IListSource;
if (listSource != null)
list = listSource.GetList();
else
list = dataGrid.DataSource as IList;
return list;
}
|
|
|
|
 |
|
 |
Thanks for this article Tom. It worked like a charm! No warning, no cast errors!!!
In this open world, who needs WINDOWS and GATES?
|
|
|
|
 |
|
 |
When using this routine, my bit fields in the datagrid no longer are shown as checkboxes.
|
|
|
|
 |
|
 |
i am developing a VB.NET application using C# and trying to customize DataGrid Header Text so that if the header text of the dataGrid is large then it should appear in next line but not able to fine any.got some sample code but they are all about to adjust the length of the header text so that the text accomodate properly but i want a multiline DataGrid Header text. Thanks In Advance Shan
|
|
|
|
 |
|
 |
This is also an issue for the project I am working on - except I need to wrap text lines in a text column within the grid. I need for the rows to be fixed height with bound text in them. This works great but the text does not wrap unless you select the text and then it wraps. I need something that will automatically wrap the text.
Thanks in advance for any suggestions.
|
|
|
|
 |
|
 |
Hallo,
what I have to consider, if I want to implement this source for a DataGrid
in a WebApplication, because it doesn't work in Web.Especially the method CreateGraphics()isn't recognized.
Thanks for helping me
|
|
|
|
 |
|
 |
I needed this for a Windows Forms application so I wouldn't know. Hopefully, someone else here can help you. If they do and publish the answer, I'll update the article for future reference.
Cheers,
Tom Archer - Archer Consulting Group
Programmer Trainer and Mentor and Project Management Consultant
|
|
|
|
 |
|
 |
hey,
good code, however...
how can i stop the code from adjusting the colors I set thru the IDE on the datagrid? it resets to a default gray style.
|
|
|
|
 |
|
 |
Hi
When in the IDE add a tablestyle to the tablestyles collection on the grid properties.
then find
// Define new table style.
DataGridTableStyle tableStyle = new DataGridTableStyle();
in the provided code and change to
// Define new table style.
DataGridTableStyle tableStyle = dataGrid.TableStyles[0];
// this will use the style of the grid that you have defined and everything else should work
Chris
|
|
|
|
 |
|
 |
On a simular piece of code that I wrote, I did this and that problem was solved. Hope it helps:
dataGridTableStyle.AlternatingBackColor = grid.AlternatingBackColor;
dataGridTableStyle.BackColor = grid.BackColor;
dataGridTableStyle.ForeColor = grid.ForeColor;
dataGridTableStyle.GridLineColor = grid.GridLineColor;
dataGridTableStyle.HeaderBackColor = grid.HeaderBackColor;
dataGridTableStyle.HeaderFont = grid.HeaderFont;
dataGridTableStyle.HeaderForeColor = grid.HeaderForeColor;
dataGridTableStyle.LinkColor = grid.LinkColor;
dataGridTableStyle.SelectionBackColor = grid.SelectionBackColor;
dataGridTableStyle.SelectionForeColor = grid.SelectionForeColor;
dataGridTableStyle.PreferredRowHeight = grid.PreferredRowHeight;
It ain't pretty, but it works.
www.CoderForRent.com
|
|
|
|
 |
|
 |
It could be possible to add some configurability to this thing. For example, the largest column could be resized so that, if possible, the right-most column ends on the right-side of the datagrid, avoiding the ugly grey background to be shown...
Life would be so much easier if we could look at the source code...
|
|
|
|
 |
|
 |
Thank you^^*
seungpil-Yoo
|
|
|
|
 |
|
 |
This is great. It gives us a foundation for manipulating the dreaded Data Grid control. Thanks again for showing the way, just like your book right on time.;P
|
|
|
|
 |
|
 |
Sorry about my English... I'm learning C#.. I have this code....
OleDbDataAdapter custDA = new OleDbDataAdapter(textBox1.Text, cn);
//textBox1 have a Shape SQL
DataSet custDS = new DataSet();
custDA.Fill(custDS, "Customers");
this.myGrid.DataSource = custDS.Tables[0].DefaultView;
SizeColumnsToContent(myGrid, -1);
But in
DataTable dataTable = (DataTable)dataGrid.DataSource;
I have an error : Specified Cast is not valid...
Why???
Sashka
|
|
|
|
 |
|
 |
There are two objects: DataView and DataTable; you can easily get one from another but formally types are different. DataSource property is DataView, so you have to cast like this:
dataTable = ((DataView)dataGrid.DataSource).Table
I-e-e-h, Sashka! Uchi matchast...
Good luck!
|
|
|
|
 |
|
 |
Thanks )
Sashka
|
|
|
|
 |
|
 |
To anyone out there who's still getting the cast error (I know I was):
if your underlying datasource is a DataSet then you need to cast it as DataSet, rather than DataView, and grab one of it's tables.
examples:
dataTable = ((DataSet)dataGrid.DataSource).Tables[0]; //for the 1st table
dataTable = ((DataSet)dataGrid.DataSource).Tables["MyTable"]; //for table by name
DataSource is really a catch-all for a DataTable, DataView, DataSet, or DataViewManager (never messed with that one myself). So the trick is to map from whatever your DataSource type is "under the hood" to a table.
hope this helps
-Darrell
hardly a .Net guru ... just learning as I go here
|
|
|
|
 |
|
 |
I had the same problem and tried many things and still would not work.
It may be because I'm using ver 2002.
Anyway, this is what I did to fix mine:
I declared somewhere:
dataGrid1.DataSource = myDS.Tables[MyTableName];
...
SizeColumnsToContent(dataGrid1,myDS.Tables[MyTableName];,-1);
Then added one more parameter to the function:
public void SizeColumnsToContent(DataGrid dataGrid, DataTable dataSourceTbl, int nRowsToScan)
{
...
DataTable dataTable = dataSourceTbl;
...
}
|
|
|
|
 |
|
 |
Sorry about not responding sooner. I just got back into town from a training seminar I was giving. Anyway, I'm glad to see your query was answered.
Cheers,
Tom Archer
Inside C#, Extending MFC Applications with the .NET Framework
It's better to listen to others than to speak, because I already know what I'm going to say anyway. - friend of Jörgen Sigvardsson
|
|
|
|
 |