Results 1 to 7 of 7

Thread: DataGrid

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2008
    Posts
    1,260

    DataGrid

    Is it possible to change the background color of a cell when I click on it?

    Also, is it possible to set a property so that when a cell is clicked on once it already has the focus that the 'edit' text feature doesn't let the user to change the text?

    Thanks

  2. #2
    Lively Member
    Join Date
    Apr 2011
    Location
    At home
    Posts
    64

    Re: DataGrid

    Quote Originally Posted by Simon Canning View Post
    Is it possible to change the background color of a cell when I click on it?

    Also, is it possible to set a property so that when a cell is clicked on once it already has the focus that the 'edit' text feature doesn't let the user to change the text?

    Thanks
    For changing cell properties:

    Click on the datagrid control;
    In the properties, you will find:
    RowDefaultCellStyle: DataGridViewCellStyle { } - when you click on it a button will appear with '...' dots on it. - click on the button, and there you'll find all properties you need.

    For the second question (editing):
    Click on the data grid control in the VB (Not while debugging)
    a Play icon will be visible on the top right of the control ->
    Click the icon and select Edit Culomns -> Select the column you want to disable editing on -> Read Only -> True.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2008
    Posts
    1,260

    Re: DataGrid

    For the background color of a cell, can i change each cell individually at run time?

    I have tried this:
    Code:
    dataGridShifts.CurrentCell.DataGridView.DefaultCellStyle.BackColor = Color.Red
    But it isn't working as I want.

    Also, how can i return the value of the current selected row when I click on a cell?
    Last edited by Simon Canning; Jun 13th, 2011 at 04:23 AM.

  4. #4
    Lively Member
    Join Date
    Apr 2011
    Location
    At home
    Posts
    64

    Re: DataGrid

    Have you tried this?

    DataGridView1.CurrentCell.Style.BackColor = Color

    -

    DataGridView.Item(Column Index, Row Index) - has many properties to it, that will help you find either the value of the selected cell etc...

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2008
    Posts
    1,260

    Re: DataGrid

    OK, when i click on a cell, how can i retrieve the value of the selected cell and the row?

    thanks

  6. #6
    Lively Member
    Join Date
    Apr 2011
    Location
    At home
    Posts
    64

    Re: DataGrid

    To get all the data you need from a row:

    Steps:

    1. Get row index.

    by the CurrentRow.Index option.

    So setting row index into variable:

    dim ri = Datagridview.CurrentRow.Index

    Then use the DataGridView1.Item(0,ri).Value

  7. #7
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: DataGrid

    Quote Originally Posted by Simon Canning View Post
    OK, when i click on a cell, how can i retrieve the value of the selected cell and the row?

    thanks
    For all cells
    Code:
            Dim rowString = (From cell As DataGridViewCell In _
                             DataGridView4.Rows(DataGridView1.CurrentRow.Index) _
                             .Cells.Cast(Of DataGridViewCell)() _
                             Select cell.Value).ToArray
    
            MsgBox(String.Join(","c, Array.ConvertAll(rowString, Function(o) o.ToString)))
    For just the current selected cell
    Code:
    Console.WriteLine(DataGridView1.CurrentCell.Value)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width