Ask a questionAsk a question
 

QuestionNewby Question : Reading data from the DataGridView

  • Monday, April 30, 2007 6:46 AMSteven Hawkes Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hi,

     

    I have played with the UI Automation API and have no issue with driving an application

    but I have been unable to determine how to read data from controls using the API.   In particuliar

    I would like to be able to read data from the DataGridView.

     

    Could anyone provide any information on how this is acheived or can you point me in the

    direction of some documentation on this issue.

     

    TIA


    Steve

All Replies

  • Monday, April 30, 2007 8:18 PMPeter Donnelly - MSFT UE Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
  • Wednesday, May 02, 2007 6:54 AMSteven Hawkes Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Thanks for the information I am a little further forward.

     

    I have been investigating the DataGridView control and found some

    interesting information which i dont understand.

     

    Checking with UISpy, I see that DataGridView uses the control pattern Table. However, when I try to access it,

    I get the error that  the Table Pattern is not supported by the DataGridView control but UISpy reports that the

    DataViewGrid does support the control type Table but does not report any Control Patterns.

     

    It then gets interesting when I populate the grid with some data and run UISpy on a data cell. It reports

    that for a cell, the Control Type is ControlType.Custom and supported Control Patterns as being "Invoke" and Value.

    but the cell does not have an automation ID so how can I access it?

     

    Can anyone shed any light on this issue.  I am new to UI Automation so apologise if there is an obvious solution

    to my problem

  • Thursday, May 10, 2007 8:41 PMKarl BridgeMSFTUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    You may be experiencing an issue with a poor UI Automation implementation for that particular control.

     

    Using the AutomationID to find elements in the UI Automation tree (unless you are also implementing the control) is not recommended. The results can be unpredictable. Please see http://msdn2.microsoft.com/en-us/library/ms752331.aspx and http://msdn2.microsoft.com/en-us/library/aa349646.aspx for more information.

     

    If these documents don't help, can you provide a little more information about your scenario?

     

     

  • Friday, May 11, 2007 6:30 AMjayakhanna Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Encountered the same issue while automating the Visual Studio 2005

     

    Scenario is

    View -> Server Explorer

    right Click on the "Data Connections"

    Give valid entries in the "Add Connection" Dialog box which appears

    and then a corresponding entry would be displayed in the Server Explorer Pane

    Choose a table underneath that

    And right click and select new query and select a table

     

    A Result Pane would appear which neither have the IsGridPattern nor IsGridItemPattern

    similar is the case with Table controls also

     

    I did some workaround by click on one cell and navigating, which is very tedious

     

    ~JK

  • Friday, May 11, 2007 3:30 PMAnita Tadhani Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    You can use name property.

    please read "AutomationID for winform listbox item"

  • Saturday, May 12, 2007 5:29 AMSteven Hawkes Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    I finally manaed to get something working and the code I use is shown below.

    What I found out was that the Grid Cell is of type CustomControl and the name is

    of the format <Col String Name > < "Row"> < null indexed row>.

     

    So I have a GetAt and SetAt method that takes the grid Ui Element and searches for

    the cells based on the above.

     

    This works a charm.   I am not to happy as it is not language nuttral i.e. if the column

    name is translated in a resource table then the code breaks but for now it does work.

     

    Hope it helps others

     

     

     

    Code Snippet

    /// <summary>

    /// This method provides a cell representation of the cell content

    /// </summary>

    /// <param name="row">A zero indexed int value for the Row</param>

    /// <param name="col">A string name of the column to use </param>

    /// <returns></returns>

    public string GetAt(int row, string col)

    {

    if (null == RootElement)

    {

    throw new ArgumentNullException("Grid", Resources.ParamIsNull);

    }

    // Construct the Grid Cell Element Name

    string name = col + " Row " + row.ToString();

    // Get the Element with the Row Col Coordinates

    UIWindow element = FindDescendantByName(name, ControlType.Custom);

    string result = null;

    try

    {

    result = UIWindow.GetValue(element);

    } catch (System.NullReferenceException )

    {

    result = null;

    }

    return result;

    }

    /// <summary>

    /// This method provides a cell representation of the cell content

    /// </summary>

    /// <param name="row">A zero indexed int value for the Row</param>

    /// <param name="col">A string name of the column to use </param>

    /// <param name="val">A string value to be stored in the cell</param>

    public void SetAt(int row, string col, string val)

    {

    if (null == RootElement)

    {

    throw new ArgumentNullException("Grid",

    Resources.ParamIsNull);

    }

    // Construct the Grid Cell Element Name

    string name = col + " Row " + row.ToString();

    // Get the Element with the Row Col Coordinates

    UIWindow element = FindDescendantByName(name, ControlType.Custom);

    UIWindow.SetValue(element, val);

    }

     

  • Friday, November 14, 2008 9:27 AMBalakj Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hi Steven,

     

    We are facing a similar issue.

    I am are tring to automate a windows forms datagridview control having some columns.

    I am trying to:

    1. select cell of row 0, col 0.

    2. After selection, cell will be a control like combo box or text box

    3. Set the value in the control.

     

    The getSupportedPatterns() of the datagridview, automation element is returning System.Windows.Automation.AutomationPattern[].

     

    Please share any pointers in doing this.

     

    Thank you,

    Balakrishna