Re: Datagrids - Rows???

From: Erwin Pant via DotNetMonster.com (forum_at_DotNetMonster.com)
Date: 03/24/05

  • Next message: David K.: "Binding and saving DataGrid object with related DataTables fields"
    Date: Thu, 24 Mar 2005 17:55:29 GMT
    
    

    Why don't you want to add a ReadOnly column to your table. Here's an
    example that I tested and works:

    In your Database:
    -----------------
    - I have a table tblNames with ID, FirstName and LastName
    - Add a ReadOnly column of type Bit (assuming you're using SQL Server)
    - Update all rows that you want flagged as "ReadOnly"

    You should get the results (for example):

    ID FirstName LastName ReadOnly
    ---- ----------- ----------- --------
    1 Joe Shmoe 0
    2 John Doe 1
    3 Jane Doe 1
    4 Homer Simpson 0

    In your program:
    ----------------
    - Add a DataGrid
    - Add your table styles
    - Add the following code:

    Imports System.Data.SqlClient

    Public Class Form1
        Inherits System.Windows.Forms.Form

        Private strCnn As String = "integrated security=SSPI;data source=" +
    {Place your data source here} + ";persist security info=False;initial
    catalog=" + {Place your database here}"
        Dim sqlCn As SqlConnection = New SqlConnection(strCnn)
        Dim da As SqlDataAdapter = New SqlDataAdapter("SELECT * FROM tblNames",
    sqlCn)
        Dim ds As New DataSet

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

            sqlCn.Open()
            da.Fill(ds, "Names")
            sqlCn.Close()
            DataGrid1.DataSource = ds

        End Sub

        Private Sub DataGrid1_CurrentCellChanged(ByVal sender As Object, ByVal
    e As System.EventArgs) Handles DataGrid1.CurrentCellChanged
            DataGrid1.ReadOnly = ds.Tables(0).Rows
    (DataGrid1.CurrentCell.RowNumber).Item("ReadOnly") = True
        End Sub
    End Class

    ** In the code, when the CurrentCellChange event is triggered, I am
    checking the ReadOnly column of the dataset. If it is true, then I make
    the ENTIRE GRID read-only. When I change to a row where ReadOnly is false,
    I set the grid's ReadOnly property to false.

    It's quick and it's simple !

    Cheers !

    E

    -- 
    Message posted via http://www.dotnetmonster.com
    

  • Next message: David K.: "Binding and saving DataGrid object with related DataTables fields"