ReadOnly look and feel in DataGrid

Discussion in 'Microsoft Dot NET' started by SgeM, Sep 24, 2003.

  1. SgeM

    SgeM Guest

    I need to show some data in a readonly DataGrid. I was easily able to
    make the grid readonly (DataGrid1.ReadOnly = True) so that data could
    not be changed. But grid cells still go in edit mode (i.e., text is
    selected and cursor is blinking) giving the wrong impression that the
    text can be edited. This is in my opinion a design flow, BTW.

    If I lock it it looks fine, but then I can't select cells, rows, or
    cols, so no good either.

    I'm still pretty new at this, so please be clear and thorough in your
    reply. Sample code is highly appreciated.
    Thanks
    Simon
     
    SgeM, Sep 24, 2003
    #1
    1. Advertisements

  2. Hi SgeM,

    Thanks for your post!

    Based on my understanding, What you want is to prevent the DataGridCell
    into the edit mode(or style) when a user selects it. To achieve this
    purpose, you can change the property of
    DataGridTextBoxColumn.TextBox.Enable to "False".

    The following codes will do this job.

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load

    ' Retrieve data from the database.
    Dim con As New
    SqlConnection("server=MyServer;uid=sa;pwd=sa;database=northwind")
    Dim daEmp As New SqlDataAdapter("Select * From customers", con)
    Dim ds As New DataSet()
    daEmp.Fill(ds, "customers")

    Dim tableStyle As New DataGridTableStyle()
    tableStyle.MappingName = "customers"

    Dim DataGridTBXColumn As DataGridTextBoxColumn = Nothing

    Dim i As Integer
    Dim numCols As Integer = ds.Tables("customers").Columns.Count

    For i = 0 To numCols - 1
    DataGridTBXColumn = New DataGridTextBoxColumn()
    DataGridTBXColumn.TextBox.Enabled = False
    DataGridTBXColumn.TextBox.BackColor = System.Drawing.Color.White
    DataGridTBXColumn.HeaderText =
    ds.Tables("customers").Columns(i).ColumnName
    DataGridTBXColumn.MappingName =
    ds.Tables("customers").Columns(i).ColumnName
    tableStyle.GridColumnStyles.Add(DataGridTBXColumn)
    Next

    DataGrid1.TableStyles.Clear()
    DataGrid1.TableStyles.Add(tableStyle)

    DataGrid1.DataSource = ds.Tables("customers")
    End Sub
    End Class

    If you have any Qs, please reply to this post.



    --
    Parker Zhang
    Microsoft Developer Support

    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Parker Zhang [MSFT], Sep 25, 2003
    #2
    1. Advertisements

  3. SgeM

    SgeM Guest

    Parker,
    I tried the code and although it works, it kind of screws up the
    colors: on MouseDown the cell text turns grey. Also the line:

    >DataGridTBXColumn.TextBox.BackColor = System.Drawing.Color.White


    forces one bg color which is a problem when using alternating
    backgrounds.

    Can you think of any other way, such as trapping clicks and keydowns
    and disable edit at that point ? I tried with no success.

    Thanks again!
    Simon

    On Thu, 25 Sep 2003 11:10:01 GMT,
    (Parker Zhang [MSFT]) wrote:

    >Hi SgeM,
    >
    >Thanks for your post!
    >
    >Based on my understanding, What you want is to prevent the DataGridCell
    >into the edit mode(or style) when a user selects it. To achieve this
    >purpose, you can change the property of
    >DataGridTextBoxColumn.TextBox.Enable to "False".
    >
    >The following codes will do this job.
    >
    >Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
    >System.EventArgs) Handles MyBase.Load
    >
    > ' Retrieve data from the database.
    > Dim con As New
    >SqlConnection("server=MyServer;uid=sa;pwd=sa;database=northwind")
    > Dim daEmp As New SqlDataAdapter("Select * From customers", con)
    > Dim ds As New DataSet()
    > daEmp.Fill(ds, "customers")
    >
    > Dim tableStyle As New DataGridTableStyle()
    > tableStyle.MappingName = "customers"
    >
    > Dim DataGridTBXColumn As DataGridTextBoxColumn = Nothing
    >
    > Dim i As Integer
    > Dim numCols As Integer = ds.Tables("customers").Columns.Count
    >
    > For i = 0 To numCols - 1
    > DataGridTBXColumn = New DataGridTextBoxColumn()
    > DataGridTBXColumn.TextBox.Enabled = False
    > DataGridTBXColumn.TextBox.BackColor = System.Drawing.Color.White
    > DataGridTBXColumn.HeaderText =
    >ds.Tables("customers").Columns(i).ColumnName
    > DataGridTBXColumn.MappingName =
    >ds.Tables("customers").Columns(i).ColumnName
    > tableStyle.GridColumnStyles.Add(DataGridTBXColumn)
    > Next
    >
    > DataGrid1.TableStyles.Clear()
    > DataGrid1.TableStyles.Add(tableStyle)
    >
    > DataGrid1.DataSource = ds.Tables("customers")
    > End Sub
    >End Class
    >
    >If you have any Qs, please reply to this post.
     
    SgeM, Sep 26, 2003
    #3
    1. Advertisements

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

    readonly for combobox and datetimepicker

    Guest, Aug 31, 2004, in forum: Microsoft Dot NET
    Replies:
    1
    Views:
    1,865
    Guest
    Aug 31, 2004
  2. Guest

    const and static readonly - Difference.

    Guest, Dec 7, 2004, in forum: Microsoft Dot NET
    Replies:
    5
    Views:
    209
    Guest
    Dec 7, 2004
  3. Guest
    Replies:
    1
    Views:
    135
    david
    Jan 20, 2005
  4. Pascal Cloup

    instaler look and feel

    Pascal Cloup, Dec 30, 2006, in forum: Microsoft Dot NET
    Replies:
    0
    Views:
    145
    Pascal Cloup
    Dec 30, 2006
  5. Simon Woods
    Replies:
    0
    Views:
    206
    Simon Woods
    Sep 20, 2008
Loading...

Share This Page