datagridview cell - get row/column info for updating data- how?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rich

    datagridview cell - get row/column info for updating data- how?

    Hello,

    If I want to update data displayed in a datagrideview/datagridview cell, how
    can I determine what cell I am updating? I am looking at the click event
    below, for example. Can I get information from the sender object or the
    EventArgs? How?

    Private Sub DataGridView1_C lick(ByVal sender As Object, ByVal e As
    System.EventArg s) Handles DataGridView1.C lick
    Console.WriteLi ne("row number of cell is ?")
    Console.Writeli ne("Column Name of Cell is ?")
    End Sub

    Thanks,
    Rich

  • james

    #2
    Re: datagridview cell - get row/column info for updating data- how?


    "Rich" <Rich@discussio ns.microsoft.co m> wrote in message
    news:410BA748-E06E-409F-9F6D-CF418C9B0A71@mi crosoft.com...[color=blue]
    > Hello,
    >
    > If I want to update data displayed in a datagrideview/datagridview cell,
    > how
    > can I determine what cell I am updating? I am looking at the click event
    > below, for example. Can I get information from the sender object or the
    > EventArgs? How?
    >
    > Private Sub DataGridView1_C lick(ByVal sender As Object, ByVal e As
    > System.EventArg s) Handles DataGridView1.C lick
    > Console.WriteLi ne("row number of cell is ?")
    > Console.Writeli ne("Column Name of Cell is ?")
    > End Sub
    >
    > Thanks,
    > Rich
    >[/color]

    I did this by having an event handler of:

    private sub dViewWeek_CellM ouseDown(ByVal sender As Object, ByVal e as
    DataGridViewCel lMouseEventArgs )

    then you get e.ColumnIndex and e.RowIndex




    Comment

    • Rich

      #3
      Re: datagridview cell - get row/column info for updating data- how

      Thanks, yes. I tried something similar which also gave me some values for e:

      Private Sub DataGridView1_C ellEnter(ByVal sender As Object, ByVal e As
      System.Windows. Forms.DataGridV iewCellEventArg s) Handles
      DataGridView1.C ellEnter
      Console.WriteLi ne(DataGridView 1.Columns(e.Col umnIndex).Name. ToString
      & " " & DataGridView1.R ows(e.RowIndex) .ToString)
      Console.WriteLi ne("*" &
      DataGridView1.R ows(e.RowIndex) .Cells(e.Column Index).Value.To String & "*")

      End Sub


      I guess I will just have to play around with the datagridview to get the
      hang of it. Would you know how to individually format a cell in a datagrid
      view? I noticed that if I do this:

      Private Sub DataGridView1_C ellFormatting(B yVal sender As Object, ByVal e As
      System.Windows. Forms.DataGridV iewCellFormatti ngEventArgs) Handles
      DataGridView1.C ellFormatting
      e.CellStyle.For eColor = Color.Red
      e.CellStyle.Bac kColor = Color.Yellow
      End Sub

      all the cells in the grid have red text and a yellow background. How can I
      do that only to the cell that I click?


      "james" wrote:
      [color=blue]
      >
      > "Rich" <Rich@discussio ns.microsoft.co m> wrote in message
      > news:410BA748-E06E-409F-9F6D-CF418C9B0A71@mi crosoft.com...[color=green]
      > > Hello,
      > >
      > > If I want to update data displayed in a datagrideview/datagridview cell,
      > > how
      > > can I determine what cell I am updating? I am looking at the click event
      > > below, for example. Can I get information from the sender object or the
      > > EventArgs? How?
      > >
      > > Private Sub DataGridView1_C lick(ByVal sender As Object, ByVal e As
      > > System.EventArg s) Handles DataGridView1.C lick
      > > Console.WriteLi ne("row number of cell is ?")
      > > Console.Writeli ne("Column Name of Cell is ?")
      > > End Sub
      > >
      > > Thanks,
      > > Rich
      > >[/color]
      >
      > I did this by having an event handler of:
      >
      > private sub dViewWeek_CellM ouseDown(ByVal sender As Object, ByVal e as
      > DataGridViewCel lMouseEventArgs )
      >
      > then you get e.ColumnIndex and e.RowIndex
      >
      >
      >
      >
      >[/color]

      Comment

      • Guest's Avatar

        #4
        Re: datagridview cell - get row/column info for updating data- how


        "Rich" <Rich@discussio ns.microsoft.co m> wrote in message
        news:40D99F74-7215-4A8C-9B80-2A31A04E3113@mi crosoft.com...[color=blue]
        > Thanks, yes. I tried something similar which also gave me some values for
        > e:
        >
        > Private Sub DataGridView1_C ellEnter(ByVal sender As Object, ByVal e As
        > System.Windows. Forms.DataGridV iewCellEventArg s) Handles
        > DataGridView1.C ellEnter
        >
        > Console.WriteLi ne(DataGridView 1.Columns(e.Col umnIndex).Name. ToString
        > & " " & DataGridView1.R ows(e.RowIndex) .ToString)
        > Console.WriteLi ne("*" &
        > DataGridView1.R ows(e.RowIndex) .Cells(e.Column Index).Value.To String & "*")
        >
        > End Sub
        >
        >
        > I guess I will just have to play around with the datagridview to get the
        > hang of it. Would you know how to individually format a cell in a
        > datagrid
        > view? I noticed that if I do this:
        >
        > Private Sub DataGridView1_C ellFormatting(B yVal sender As Object, ByVal e
        > As
        > System.Windows. Forms.DataGridV iewCellFormatti ngEventArgs) Handles
        > DataGridView1.C ellFormatting
        > e.CellStyle.For eColor = Color.Red
        > e.CellStyle.Bac kColor = Color.Yellow
        > End Sub
        >
        > all the cells in the grid have red text and a yellow background. How can
        > I
        > do that only to the cell that I click?
        >
        >[/color]

        Forgive the C# code - but you should be able to use something like:

        DataGridViewCel lStyle myNewStyle = new DataGridViewCel lStyle();
        myNewStyle.Back Color = Color.Red;
        myNewStyle.Fore Color = Color.Blue;
        dataGridView1.R ows[0].Cells[2].Style = myNewStyle;


        Comment

        Working...