Home Forums WinForms controls Xceed Grid for WinForms Get the value of a Cell ?

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #13040 |

    Hi
    how would i get the value of a cell of a record and assign it to a variable.
    Like Microsoft grids, they have the Index property where u can get the value of a specific column of the record (row) the user has clicked on. I need to get say my clientid which is in column 3 of my grid, to use it in another sql qry to the databse to display more info for that specific clientid in my second grid

    thanks

    Imported from legacy forums. Posted by FAS (had 6242 views)

    User (Old forums)
    Member
    Post count: 23064

    FAS:

    Are you using unbound or bound data?

    If you use unbound data, then you could do like this:

    foreach (Xceed.Grid.DataRow row in this.grid1.gridControl1.DataRows)
    {
    foreach (Xceed.Grid.Cell cell in row.Cells)
    {

    if (cell.FieldName == “Clientid”)
    {
    string id = (string)cell.Value;
    break;
    }
    }
    }

    Imported from legacy forums. Posted by C# (had 332 views)

    User (Old forums)
    Member
    Post count: 23064

    to get the cell value when a user click in the cell, you need to subscribe to the cell’s Click event, like:

    foreach( Cell cell in this.gridControl1.DataRowTemplate.Cells )
    {
    cell.Click -= new EventHandler(cell_Click);
    }

    private void cell_Click(object sender, EventArgs e)
    {

    //Gets the field name of the cell’s parent column.
    if (((Xceed.Grid.DataCell)sender).FieldName == “ClientID”)
    {
    string clientID = (string)((Xceed.Grid.DataCell)sender).ParentRow.Cells[“ClientID”].Value;
    }

    }

    Regards
    C#

    Imported from legacy forums. Posted by C# (had 542 views)

    User (Old forums)
    Member
    Post count: 23064

    The previous reply showed you how to get the value of a cell using c# – here’s how I do it using VB.NET (Yes, VB.NET allows you to stack a ton of functions onto the same line, identical to C# but I’m “old school” and prefer looking at code that’s easy to read…so you’ll have to excuse my older way of writing!)

    FYI – this will work with bound or unbound…makes no difference.

    ‘ This first function sets up the Grid to handle a couple of events…in this case, a single and a double-click

    Private Sub InitGrid()
    ‘ Right-Click event handler
    AddHandler colTemplate_RunNumber.MouseDown, AddressOf runExp_Click
    AddHandler colTemplate_RunName.MouseDown, AddressOf runExp_Click

    ‘ Double-Click event handler
    AddHandler colTemplate_RunNumber.DoubleClick, AddressOf runExp_DoubleClick
    AddHandler colTemplate_RunName.DoubleClick, AddressOf runExp_DoubleClick
    End Sub

    ‘ Here’s the Single-Click routine. In my ap, I have a context-menu that pops up so I’m only trapping the right-click here.
    Private Sub runExp_Click(ByVal sender As Object, ByVal e As System.windows.Forms.MouseEventArgs)
    Select Case e.Button
    Case MouseButtons.Right
    Dim reCellRow As CellRow
    Dim reGridRow As Xceed.Grid.DataRow
    reCellRow = (CType(sender, Cell).ParentRow)

    ‘ Select the row they clicked on…
    grdRunExp.SelectedRows.Clear()
    grdRunExp.SelectedRows.Add(reCellRow)
    grdRunExp.CurrentRow = reCellRow

    ‘ Demo getting s value from a Cell on a single-click
    Dim sClient As String
    Dim sRunNumber as String

    ‘ Here’s a value in that Row under the “RunNumber” column
    sRunNumber = CType(reCellRow.Cells(“RunNumber”).Value, String)

    ‘ Here’s another value in that same Row, under the “ClientName” column
    sClient = CType(reCellRow.Cells(“ClientName”).Value, String)

    Case Else
    End Select
    End Sub

    ‘ More of the same, but demos the trapping of the double-click.
    Private Sub runExp_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim reCellRow As CellRow
    Dim sClient As String
    Dim sRunNumber as String

    ‘ This the row of the cell that was clicked on
    reCellRow = (CType(sender, Cell).ParentRow)

    ‘ Here’s a value in that Row under the “RunNumber” column
    sRunNumber = CType(reCellRow.Cells(“RunNumber”).Value, String)

    ‘ Here’s another value in that same Row, under the “ClientName” column
    sClient = CType(reCellRow.Cells(“ClientName”).Value, String)
    End Sub

    Hope this helps!

    -LK

    Imported from legacy forums. Posted by Curtis (had 4413 views)

    User (Old forums)
    Member
    Post count: 23064

    I tried this solution but it does not seem to work. I receive the following message. “value is not a memeber of xceed.wpf.datagrid.cell”

    Has something changed since this was posted?

     

    Thanks

    Imported from legacy forums. Posted by Adam (had 345 views)

    User (Old forums)
    Member
    Post count: 23064

    This is a thread of the Xceed Grid for .NET, not for the Xceed DataGrid for WPF, which is why you don’t find this property on the cell class of the WPF grid.  I suggest you look at the Dforums :

    http://xceed.com/CS/forums/35/ShowForum.aspx

    Imported from legacy forums. Posted by André (had 3779 views)

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.