Making datagrid partially editable

S

Stephan Bour

Hi,
I have a datagrid databound to a SQL query. I'd like to allow editing of
some columns but not all. Is there a way to turn off the conversion of the
datagrid cells to textboxes for some columns when the Edit button is
pressed?
Thank you,
Stephan.
 
M

Morgan

Convert the columns to templates and the columns you don't want to be
editable, change the edit item template control from a textbox to a label.

--Morgan
 
S

Stephan Bour

Thanks for the suggestion. If I simply add a template column with a label, I
do get a non-editable column. Unfortunately, that doesn't prevent the
EditCommandColumn to generate all columns contained in my databinder and
making them editable. In the example below, the "Description" column is
duplicated with one editable and the other (the itemtemplate one) not. Am I
going to have to modify the SQL query or is there a way to exclude the
Description column from the EditCommandColumn?


<asp:DataGrid id="ByJob" AutoGenerateColumns="true"
OnEditCommand="ByJob_Edit" OnCancelCommand="ByJob_Cancel"
OnUpdateCommand="ByJob_Update" runat="server">
<HeaderStyle backcolor="Black" forecolor="White" font-bold="True"
horizontalalign="Left" />
<Columns>
<asp:EditCommandColumn EditText="Edit"
CancelText="Cancel"
UpdateText="Update"
ItemStyle-Wrap="false"
/>

<asp:TemplateColumn>

<HeaderTemplate>
<b> Description </b>
</HeaderTemplate>

<ItemTemplate>
<asp:Label
Text='<%# DataBinder.Eval(Container.DataItem,
"Description") %>'
runat="server"/>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>

Thanks,
Stephan.
 
M

Morgan

Not at all. My suggestion was to convert the existing column to a template
column. Once you do this, you have 2 different items to work with (right
click the grid, Edit Template), the Item Template and the EditItemTemplate.
The Item Template is the read-only portion, EditItem Template is what is
displayed when you set the edit item index. Example below.

After all this, I remember you can set the Read Only property on the column
in the designer, which should do the trick for you and is much easier to
implement. Hopefully that will give you what you need.

<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:Label id="LabelReadOnlyDisplayMode"
runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label id="LabelReadOnlyEditMode"
runat="server"></asp:Label>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>

Morgan
 
S

Stephan Bour

Got it! Thanks a lot.
Stephan.

Not at all. My suggestion was to convert the existing column to a template
column. Once you do this, you have 2 different items to work with (right
click the grid, Edit Template), the Item Template and the EditItemTemplate.
The Item Template is the read-only portion, EditItem Template is what is
displayed when you set the edit item index. Example below.

After all this, I remember you can set the Read Only property on the column
in the designer, which should do the trick for you and is much easier to
implement. Hopefully that will give you what you need.

<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:Label id="LabelReadOnlyDisplayMode"
runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label id="LabelReadOnlyEditMode"
runat="server"></asp:Label>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>

Morgan
 

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

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top