DataGrid Column Resizing

  • Thread starter Thread starter Pramod
  • Start date Start date
P

Pramod

Hi All,

I am working in C# .Net and have created a table using the DataGrid.
Now I want to change the size of the column when the user enters some text
in the Cell only if the Text entered exceeds the cell bounds.

I need help.
Urgent!!!!!!!!!!!

Plz if any of you could help me.

Regards
Pramod
 
Hi Vuong,
But there isn't any TextChanged event for the DataGrid.
I have tried to change the width of the column by trapping the KeyPress
event but that doesn't give me the effect as the table of MS-Word has.

if You find anything related to this plz reply

Regards ,
Pramod
 
Yes, KeyPressed even doesn't occuers when you editing some value in DataGrid.
But if you want, you can hadle KeyPressed event from parent form (that
conatains your DataGrid). To do that just create a handler for KeyPressed
event in our form and set form's KeyPreview property to true.
 
Hi Andry,
I have used ColumnStyles and TextBoxColumns and handled the KeyPress event
but now the problem is if I increase the width of the column the earlier
Text gets cleared as the width goes on increasing. i.e. If i have entered
"Andry" and the text now exceeds the width of the Column size the column
size increases but "Andry" gets deleted and whichever alphabet i type next
is seen.

I also want the size to increase in width to some extent and then the Height
should increase.
You just create a table in MS-WORD and try this
It will be better for u to understand what i Want to do.
I want the same functionality that word provides with the table

plz reply soon

Regards

Pramod
 
Hi

You will have to write a class inherited from
System.Windows.Forms.DataGridTextBoxColumn with overloaded Edit method. In
this metod you shoud calculate a size of string with Graphics.MeasureString
and set this value to Witdth property...
 
Hi Andry,
Thanks for your suggestion but how should i increase the Row height, 'cos i
think increasing row height to fit the text will be easier than changing the
width of all the columns.

Waiting for ur suggestions and some sample code


Regards
Pramod
 
Possibly you create new instance of ColumnStyles or TextBoxColumns each time
you handle the event. If that is true, then try change value of existing
object insted of creating new one.
Any way you can try to remember cell value before changing width and restore
it after width is changed....

In general to achve functionality like in MS-Word you will have to write all
code by ourself. You may calc avarege width of coloumns in our datagrid and
if curent cell width is bigger then avarege*2 then increse higth insted of
future incresing width.
Or do it other way.
 
try this:
//this.DataGrid1 is my datagrid
//this.DataGridTableStyle1 is the tablestyle associated with the datagrid

this.DataGridTableStyle1.PreferredRowHeight = 20;
this.DataGrid1.TableStyles.Clear();
this.DataGrid1.TableStyles.Add(this.DataGridTableStyle1);
 
Hi Andry,

I have somehave managed to increase the width and the Height of the Table Cell,
but now the problem is that when the focus is changed to another cell, the height of the previous cell changes and gets resized to the original size.

can plz help me for this
Thanks for spending ur valuable time with me

Waitig for ur reply,
Regards,

Pramod








=?Utf-8?B?QW5kcnlfUg==?= said:
Possibly you create new instance of ColumnStyles or TextBoxColumns each time
you handle the event. If that is true, then try change value of existing
object insted of creating new one.
Any way you can try to remember cell value before changing width and restore
it after width is changed....

In general to achve functionality like in MS-Word you will have to write all
code by ourself. You may calc avarege width of coloumns in our datagrid and
if curent cell width is bigger then avarege*2 then increse higth insted of
future incresing width.
Or do it other way.
-----------------
Good luck!
Andry


"Pramod" wrote:

> Hi Andry,
> I have used ColumnStyles and TextBoxColumns and handled the KeyPress event
> but now the problem is if I increase the width of the column the earlier
> Text gets cleared as the width goes on increasing. i.e. If i have entered
> "Andry" and the text now exceeds the width of the Column size the column
> size increases but "Andry" gets deleted and whichever alphabet i type next
> is seen.
>
> I also want the size to increase in width to some extent and then the Height
> should increase.
> You just create a table in MS-WORD and try this
> It will be better for u to understand what i Want to do.
> I want the same functionality that word provides with the table
>
> plz reply soon
>
> Regards
>
> Pramod
>
>
>
>
> "Andry_R" <[email protected]> wrote in message
> news:[email protected]...
> > Yes, KeyPressed even doesn't occuers when you editing some value in

> DataGrid.
> > But if you want, you can hadle KeyPressed event from parent form (that
> > conatains your DataGrid). To do that just create a handler for KeyPressed
> > event in our form and set form's KeyPreview property to true.
> >
> > -----------------
> > Good luck!
> > Andry
> >
> >
> > "Pramod" wrote:
> >
> > > Hi Vuong,
> > > But there isn't any TextChanged event for the DataGrid.
> > > I have tried to change the width of the column by trapping the KeyPress
> > > event but that doesn't give me the effect as the table of MS-Word has.
> > >
> > > if You find anything related to this plz reply
> > >
> > > Regards ,
> > > Pramod
> > >
> > >
> > >
> > > "Vuong" <[email protected]> wrote in message
> > > news:#[email protected]...
> > > > Pramod,
> > > > You may have to take a look at text changed event.
> > > >
> > > > Vuong
> > > > Tran Xuan
> > > >
> > > > "Pramod" <[email protected]> wrote in message
> > > > news:[email protected]...
> > > > > Hi All,
> > > > >
> > > > > I am working in C# .Net and have created a table using the DataGrid.
> > > > > Now I want to change the size of the column when the user enters

> some
> > > text
> > > > > in the Cell only if the Text entered exceeds the cell bounds.
> > > > >
> > > > > I need help.
> > > > > Urgent!!!!!!!!!!!
> > > > >
> > > > > Plz if any of you could help me.
> > > > >
> > > > > Regards
> > > > > Pramod
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >
> > >

>
>
>
 
Back
Top