Home All Groups Group Topic Archive Search About

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

Author
26 May 2006 4:04 PM
Rich
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_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles DataGridView1.Click
   Console.WriteLine("row number of cell is ?")
   Console.Writeline("Column Name of Cell is ?")
End Sub

Thanks,
Rich

Author
26 May 2006 4:19 PM
james
Show quote Hide quote
"Rich" <R***@discussions.microsoft.com> wrote in message
news:410BA748-E06E-409F-9F6D-CF418C9B0A71@microsoft.com...
> 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_Click(ByVal sender As Object, ByVal e As
> System.EventArgs) Handles DataGridView1.Click
>   Console.WriteLine("row number of cell is ?")
>   Console.Writeline("Column Name of Cell is ?")
> End Sub
>
> Thanks,
> Rich
>

I did this by having an event handler of:

private sub dViewWeek_CellMouseDown(ByVal sender As Object, ByVal e as
DataGridViewCellMouseEventArgs)

then you get e.ColumnIndex and e.RowIndex
Author
26 May 2006 4:43 PM
Rich
Thanks, yes.  I tried something similar which also gave me some values for e:

    Private Sub DataGridView1_CellEnter(ByVal sender As Object, ByVal e As
System.Windows.Forms.DataGridViewCellEventArgs) Handles
DataGridView1.CellEnter
        Console.WriteLine(DataGridView1.Columns(e.ColumnIndex).Name.ToString
& "  " & DataGridView1.Rows(e.RowIndex).ToString)
        Console.WriteLine("*" &
DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString & "*")

    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_CellFormatting(ByVal sender As Object, ByVal e As
System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles
DataGridView1.CellFormatting
        e.CellStyle.ForeColor = Color.Red
        e.CellStyle.BackColor = 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?


Show quoteHide quote
"james" wrote:

>
> "Rich" <R***@discussions.microsoft.com> wrote in message
> news:410BA748-E06E-409F-9F6D-CF418C9B0A71@microsoft.com...
> > 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_Click(ByVal sender As Object, ByVal e As
> > System.EventArgs) Handles DataGridView1.Click
> >   Console.WriteLine("row number of cell is ?")
> >   Console.Writeline("Column Name of Cell is ?")
> > End Sub
> >
> > Thanks,
> > Rich
> >
>
> I did this by having an event handler of:
>
> private sub dViewWeek_CellMouseDown(ByVal sender As Object, ByVal e as
> DataGridViewCellMouseEventArgs)
>
> then you get e.ColumnIndex and e.RowIndex
>
>
>
>
>
Author
26 May 2006 7:42 PM
james
Show quote Hide quote
"Rich" <R***@discussions.microsoft.com> wrote in message
news:40D99F74-7215-4A8C-9B80-2A31A04E3113@microsoft.com...
> Thanks, yes.  I tried something similar which also gave me some values for
> e:
>
>    Private Sub DataGridView1_CellEnter(ByVal sender As Object, ByVal e As
> System.Windows.Forms.DataGridViewCellEventArgs) Handles
> DataGridView1.CellEnter
>
> Console.WriteLine(DataGridView1.Columns(e.ColumnIndex).Name.ToString
> & "  " & DataGridView1.Rows(e.RowIndex).ToString)
>        Console.WriteLine("*" &
> DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString & "*")
>
>    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_CellFormatting(ByVal sender As Object, ByVal e
> As
> System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles
> DataGridView1.CellFormatting
>        e.CellStyle.ForeColor = Color.Red
>        e.CellStyle.BackColor = 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?
>
>

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

DataGridViewCellStyle myNewStyle = new DataGridViewCellStyle();
myNewStyle.BackColor = Color.Red;
myNewStyle.ForeColor = Color.Blue;
dataGridView1.Rows[0].Cells[2].Style = myNewStyle;