By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,948 Members | 900 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,948 IT Pros & Developers. It's quick & easy.

Text Alignment in DataGrid Edit TextBox

P: n/a
When the EditCommandColumn in a DataGrid is clicked, all the
BoundColumns get replaced by TextBoxes so that users can alter the
data. By default, the Text in the TextBoxes are left-aligned. Is there
any way by which the Text in some of the TextBoxes, not all, be
center-aligned or right-aligned?

Please note that I am referring to the alignment of the Text in the
TextBoxes & NOT the alignment of the TextBoxes within the cells in the
DataGrid.

Nov 28 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Yes there is. Use a template column instead. Heres a short little
example.

<asp:TemplateColumn HeaderText="UserName">
<ItemTemplate>
<asp:Label ID="TheLabel" runat="server"><%#
DataBinder.Eval(Container.DataItem, "Data") %></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="TheTextBox" runat="server"
style="text-align:center" Text=<%# DataBinder.Eval(Container.DataItem,
"Data") %/>
</EditItemTemplate>
</asp:TemplateColumn>

The style can be set to left, right or center. Of course you can also
set the cssclass as well. This is probably a better option but it all
works the same.

You can set the itemstyle-cssclass on a bound column but this only
applies to the display of the data within a label. When you switch to
edit, the style class is not applied to the textbox.
http://localhost/TestService1/WebForm2.aspx
rn**@rediffmail.com wrote:
When the EditCommandColumn in a DataGrid is clicked, all the
BoundColumns get replaced by TextBoxes so that users can alter the
data. By default, the Text in the TextBoxes are left-aligned. Is there
any way by which the Text in some of the TextBoxes, not all, be
center-aligned or right-aligned?

Please note that I am referring to the alignment of the Text in the
TextBoxes & NOT the alignment of the TextBoxes within the cells in the
DataGrid.
Nov 29 '06 #2

P: n/a
Again it's my dear Gozirra & as expected, you have once again provided
the solution I was looking out for.

Gozirra, a DataGrid can be set in the editable mode using BoundColumns
as well as EditItemTemplate. Which one would you suggest & why? Are
there any performance issues involved using one over the other?
Gozirra wrote:
Yes there is. Use a template column instead. Heres a short little
example.

<asp:TemplateColumn HeaderText="UserName">
<ItemTemplate>
<asp:Label ID="TheLabel" runat="server"><%#
DataBinder.Eval(Container.DataItem, "Data") %></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="TheTextBox" runat="server"
style="text-align:center" Text=<%# DataBinder.Eval(Container.DataItem,
"Data") %/>
</EditItemTemplate>
</asp:TemplateColumn>

The style can be set to left, right or center. Of course you can also
set the cssclass as well. This is probably a better option but it all
works the same.

You can set the itemstyle-cssclass on a bound column but this only
applies to the display of the data within a label. When you switch to
edit, the style class is not applied to the textbox.
http://localhost/TestService1/WebForm2.aspx
rn**@rediffmail.com wrote:
When the EditCommandColumn in a DataGrid is clicked, all the
BoundColumns get replaced by TextBoxes so that users can alter the
data. By default, the Text in the TextBoxes are left-aligned. Is there
any way by which the Text in some of the TextBoxes, not all, be
center-aligned or right-aligned?

Please note that I am referring to the alignment of the Text in the
TextBoxes & NOT the alignment of the TextBoxes within the cells in the
DataGrid.
Nov 29 '06 #3

P: n/a
Hi,
I'll suggest Edit Template mode because this is designed specially for
Text box.

Anshu
rn**@rediffmail.com wrote:
Again it's my dear Gozirra & as expected, you have once again provided
the solution I was looking out for.

Gozirra, a DataGrid can be set in the editable mode using BoundColumns
as well as EditItemTemplate. Which one would you suggest & why? Are
there any performance issues involved using one over the other?
Gozirra wrote:
Yes there is. Use a template column instead. Heres a short little
example.

<asp:TemplateColumn HeaderText="UserName">
<ItemTemplate>
<asp:Label ID="TheLabel" runat="server"><%#
DataBinder.Eval(Container.DataItem, "Data") %></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="TheTextBox" runat="server"
style="text-align:center" Text=<%# DataBinder.Eval(Container.DataItem,
"Data") %/>
</EditItemTemplate>
</asp:TemplateColumn>

The style can be set to left, right or center. Of course you can also
set the cssclass as well. This is probably a better option but it all
works the same.

You can set the itemstyle-cssclass on a bound column but this only
applies to the display of the data within a label. When you switch to
edit, the style class is not applied to the textbox.
http://localhost/TestService1/WebForm2.aspx
rn**@rediffmail.com wrote:
When the EditCommandColumn in a DataGrid is clicked, all the
BoundColumns get replaced by TextBoxes so that users can alter the
data. By default, the Text in the TextBoxes are left-aligned. Is there
any way by which the Text in some of the TextBoxes, not all, be
center-aligned or right-aligned?
>
Please note that I am referring to the alignment of the Text in the
TextBoxes & NOT the alignment of the TextBoxes within the cells in the
DataGrid.
Nov 29 '06 #4

P: n/a
Rocky how do you say that the EditItemTemplate is designed specially
for TextBox? In fact, instead of having a TextBox within the
EditItemTemplate, I can even have a CheckBox within the
EditItemTemplate. So I couldn't follow what makes you say that the
EditItemTemplate is designed specially for TextBox.

In fact, on the other hand, BoundColumns will always get replaced by
TextBoxes when the DataGrid is in the editable mode. So I believe, on
the contrary, BoundColumn is specially designed for TextBox, isn't it?

Correct me if I am wrong.
Rocky wrote:
Hi,
I'll suggest Edit Template mode because this is designed specially for
Text box.

Anshu
rn**@rediffmail.com wrote:
Again it's my dear Gozirra & as expected, you have once again provided
the solution I was looking out for.

Gozirra, a DataGrid can be set in the editable mode using BoundColumns
as well as EditItemTemplate. Which one would you suggest & why? Are
there any performance issues involved using one over the other?
Gozirra wrote:
Yes there is. Use a template column instead. Heres a short little
example.
>
<asp:TemplateColumn HeaderText="UserName">
<ItemTemplate>
<asp:Label ID="TheLabel" runat="server"><%#
DataBinder.Eval(Container.DataItem, "Data") %></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="TheTextBox" runat="server"
style="text-align:center" Text=<%# DataBinder.Eval(Container.DataItem,
"Data") %/>
</EditItemTemplate>
</asp:TemplateColumn>
>
The style can be set to left, right or center. Of course you can also
set the cssclass as well. This is probably a better option but it all
works the same.
>
You can set the itemstyle-cssclass on a bound column but this only
applies to the display of the data within a label. When you switch to
edit, the style class is not applied to the textbox.
>
>
http://localhost/TestService1/WebForm2.aspx
rn**@rediffmail.com wrote:
When the EditCommandColumn in a DataGrid is clicked, all the
BoundColumns get replaced by TextBoxes so that users can alter the
data. By default, the Text in the TextBoxes are left-aligned. Is there
any way by which the Text in some of the TextBoxes, not all, be
center-aligned or right-aligned?

Please note that I am referring to the alignment of the Text in the
TextBoxes & NOT the alignment of the TextBoxes within the cells in the
DataGrid.
Nov 29 '06 #5

P: n/a
Gozirra, one thing I forgot to mention - the Text in the TextBoxes in
the BoundColumns when the DataGrid is in the editable mode can also be
center or right-aligned using this style

<style>
input{
text-align:center;
}
</style>

But the drawback of this is that all the TextBoxes in the page (be it
inside or outside the DataGrid) will have their Text aligned at the
center.
Gozirra wrote:
Yes there is. Use a template column instead. Heres a short little
example.

<asp:TemplateColumn HeaderText="UserName">
<ItemTemplate>
<asp:Label ID="TheLabel" runat="server"><%#
DataBinder.Eval(Container.DataItem, "Data") %></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="TheTextBox" runat="server"
style="text-align:center" Text=<%# DataBinder.Eval(Container.DataItem,
"Data") %/>
</EditItemTemplate>
</asp:TemplateColumn>

The style can be set to left, right or center. Of course you can also
set the cssclass as well. This is probably a better option but it all
works the same.

You can set the itemstyle-cssclass on a bound column but this only
applies to the display of the data within a label. When you switch to
edit, the style class is not applied to the textbox.
http://localhost/TestService1/WebForm2.aspx
rn**@rediffmail.com wrote:
When the EditCommandColumn in a DataGrid is clicked, all the
BoundColumns get replaced by TextBoxes so that users can alter the
data. By default, the Text in the TextBoxes are left-aligned. Is there
any way by which the Text in some of the TextBoxes, not all, be
center-aligned or right-aligned?

Please note that I am referring to the alignment of the Text in the
TextBoxes & NOT the alignment of the TextBoxes within the cells in the
DataGrid.
Nov 29 '06 #6

P: n/a
>From what I've seen there are no performance hits using the
edititemtemplate or the bound column. I usually use boundcolumns when
I have no need for anything out of the ordinary. If all you require is
simple text input, then the bound column fills the need. I use
edititemtemplate for cases where you need greater control over the
output. As you pointed out, you get greater flexibility with the
ability to use a number of different controls within the
edititemtemplate and greater control over the styles applied to those
controls. So thats my 2 cents for what its worth.

rn**@rediffmail.com wrote:
Again it's my dear Gozirra & as expected, you have once again provided
the solution I was looking out for.

Gozirra, a DataGrid can be set in the editable mode using BoundColumns
as well as EditItemTemplate. Which one would you suggest & why? Are
there any performance issues involved using one over the other?
Gozirra wrote:
Yes there is. Use a template column instead. Heres a short little
example.

<asp:TemplateColumn HeaderText="UserName">
<ItemTemplate>
<asp:Label ID="TheLabel" runat="server"><%#
DataBinder.Eval(Container.DataItem, "Data") %></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="TheTextBox" runat="server"
style="text-align:center" Text=<%# DataBinder.Eval(Container.DataItem,
"Data") %/>
</EditItemTemplate>
</asp:TemplateColumn>

The style can be set to left, right or center. Of course you can also
set the cssclass as well. This is probably a better option but it all
works the same.

You can set the itemstyle-cssclass on a bound column but this only
applies to the display of the data within a label. When you switch to
edit, the style class is not applied to the textbox.
http://localhost/TestService1/WebForm2.aspx
rn**@rediffmail.com wrote:
When the EditCommandColumn in a DataGrid is clicked, all the
BoundColumns get replaced by TextBoxes so that users can alter the
data. By default, the Text in the TextBoxes are left-aligned. Is there
any way by which the Text in some of the TextBoxes, not all, be
center-aligned or right-aligned?
>
Please note that I am referring to the alignment of the Text in the
TextBoxes & NOT the alignment of the TextBoxes within the cells in the
DataGrid.
Nov 30 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.