Text Alignment in DataGrid Edit TextBox

R

rn5a

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.
 
G

Gozirra

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
 
R

rn5a

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?
 
R

Rocky

Hi,
I'll suggest Edit Template mode because this is designed specially for
Text box.

Anshu
 
R

rn5a

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.
 
R

rn5a

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.
 
G

Gozirra

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top