DataGrid: set ReadOnly property at runtime?

Discussion in 'ASP .Net' started by John Smith, Jun 6, 2006.

  1. John Smith

    John Smith Guest

    Is it possible to set DataGrid's ReadOnly property to True/False at runtime?
    An example:

    <asp:datagrid id="dg1" runat="server" AutoGenerateColumns="False">
    <Columns>
    <asp:BoundColumn DataField="id" ReadOnly="True"
    HeaderText="ID"></asp:BoundColumn>
    </Columns>
    <asp:datagrid>
    John Smith, Jun 6, 2006
    #1
    1. Advertising

  2. John Smith

    John Smith Guest


    > Is it possible to set DataGrid's ReadOnly property to True/False at
    > runtime?


    P.S. My question was not limited to <asp:BoundColumn/>, but to
    <asp:TemplateColumn/> as well:

    <asp:datagrid id="dg1" runat="server" AutoGenerateColumns="False">
    <Columns>
    <ItemTemplate>
    <asp:Label id="lblSum" runat="server" Text='<%#
    Container.DataItem("Sum") %>'>
    </asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:TextBox id="txtSum" runat="server" Text='<%#
    Container.DataItem("Sum") %>'>
    </asp:TextBox>
    </EditItemTemplate>
    </Columns>
    </asp:datagrid>

    I would like to acomplish next: to show TextBoxes, only in some cells
    (columns), not in all.
    John Smith, Jun 6, 2006
    #2
    1. Advertising

  3. John Smith wrote:
    > Is it possible to set DataGrid's ReadOnly property to True/False at
    > runtime? An example:
    >
    > <asp:datagrid id="dg1" runat="server" AutoGenerateColumns="False">
    > <Columns>
    > <asp:BoundColumn DataField="id" ReadOnly="True"
    > HeaderText="ID"></asp:BoundColumn>
    > </Columns>
    > <asp:datagrid>


    Hmm. You asked about the grid's ReadOnly property, but you illustrated
    it by showing a column's ReadOnly attribute. Which one did you really
    mean?

    Sub SetGridEditMode(RO as boolean)
    dg1.ReadOnly = RO
    End Sub

    or

    Sub SetGridColEditMode(colidx as int32,RO as boolean)
    dim col as datagridcolumn = dg1.Columns(colidx)
    End Sub

    --
    Microsoft MVP -- ASP/ASP.NET
    Please reply to the newsgroup. The email account listed in my From
    header is my spam trap, so I don't check it very often. You will get a
    quicker response by posting to the newsgroup.
    Bob Barrows [MVP], Jun 6, 2006
    #3
  4. John Smith

    John Smith Guest


    > Hmm. You asked about the grid's ReadOnly property, but you illustrated
    > it by showing a column's ReadOnly attribute. Which one did you really
    > mean?


    Column's ReadOnly attribute.

    > Sub SetGridColEditMode(colidx as int32,RO as boolean)
    > dim col as datagridcolumn = dg1.Columns(colidx)
    > End Sub


    Column doesn't have ReadOnly attribute/property?
    John Smith, Jun 6, 2006
    #4
  5. John Smith wrote:
    >> Is it possible to set DataGrid's ReadOnly property to True/False at
    >> runtime?

    >
    > P.S. My question was not limited to <asp:BoundColumn/>, but to
    > <asp:TemplateColumn/> as well:


    My previous answer applies.

    <snip>
    >
    > I would like to acomplish next: to show TextBoxes, only in some cells
    > (columns), not in all.


    If I understand the question correctly, you will need to use templates.
    http://msdn.microsoft.com/msdnmag/issues/02/01/cutting/

    http://msdn.microsoft.com/library/e...lumnClassEditItemTemplateTopic.asp?frame=true


    Bob Barrows
    --
    Microsoft MVP -- ASP/ASP.NET
    Please reply to the newsgroup. The email account listed in my From
    header is my spam trap, so I don't check it very often. You will get a
    quicker response by posting to the newsgroup.
    Bob Barrows [MVP], Jun 6, 2006
    #5
  6. John Smith wrote:
    >> Hmm. You asked about the grid's ReadOnly property, but you
    >> illustrated it by showing a column's ReadOnly attribute. Which one
    >> did you really mean?

    >
    > Column's ReadOnly attribute.
    >
    >> Sub SetGridColEditMode(colidx as int32,RO as boolean)
    >> dim col as datagridcolumn = dg1.Columns(colidx)
    >> End Sub

    >
    > Column doesn't have ReadOnly attribute/property?


    You're right. I also missed a step:

    dim col as boundcolumn = ctype(dg1.Columns(colidx), _
    boundcolumn)
    col.readonly = RO

    --
    Microsoft MVP -- ASP/ASP.NET
    Please reply to the newsgroup. The email account listed in my From
    header is my spam trap, so I don't check it very often. You will get a
    quicker response by posting to the newsgroup.
    Bob Barrows [MVP], Jun 6, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?QmVu?=

    Set ReadOnly of DataGrid Bound column

    =?Utf-8?B?QmVu?=, Nov 12, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    439
    =?Utf-8?B?QmVu?=
    Nov 12, 2004
  2. Replies:
    0
    Views:
    409
  3. Patrik

    Set readonly property on cell level in a datagridview

    Patrik, Feb 15, 2007, in forum: ASP .Net Building Controls
    Replies:
    3
    Views:
    512
    Steven Cheng[MSFT]
    Feb 16, 2007
  4. Jonathan Hyatt
    Replies:
    1
    Views:
    1,901
    Jonathan Hyatt
    Jun 8, 2004
  5. crjunk
    Replies:
    3
    Views:
    235
    crjunk
    Jul 22, 2005
Loading...

Share This Page