CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: datagrid

  1. #1
    Join Date
    Sep 1999
    Location
    Romania
    Posts
    35

    datagrid

    I have a dataset with two tables (Category, Element) and one relation between them (every row in Category has several rows in Element). At start I saw the Category lines and clicking the (+) sign could navigate to elements.
    Problem: when a sublist of elements are shown corresponding to one catgory how can I know with wich category I work with (I need to know also the number of lines in the grid). If I want to iterate through elements I need to know the category.
    Please help me!

  2. #2
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890
    you can iterate thru all the row column,
    by

    CurrencyManager cm = (CurrencyManager)this.BindingContext[this.dataGrid1.DataSource];



    int rowCount = cm.Count;

    //assumes datasource is a datatable...

    int colCount = ((DataTable)this.dataGrid1.DataSource).Columns.Count;



    for(int row = 0; row < rowCount; row++)

    {

    for(int col = 0; col < colCount; col++)

    {

    object cellValue = this.dataGrid1[row, col];

    Console.Write(cellValue.ToString() + " ");

    }

    Console.WriteLine("");

    }



    thanx
    Paresh

  3. #3
    Join Date
    Sep 1999
    Location
    Romania
    Posts
    35
    I can not do this cast: ((DataTable)this.dataGrid1.DataSource), my datasource is a DataSet with two DataTable.

    A way to iterate my elements is:

    foreach(DataRow row in myList.Tables["myCategory"].Rows)
    {
    MessageBox.Show((string)row.ItemArray[0]);
    foreach(DataRow childRow in row.GetChildRows("myCategory_myElement"))
    {
    MessageBox.Show((string)childRow.ItemArray[0]);
    }
    }
    this gives me every element connected to the actual category.
    My problem is to find out the category when a sublist of elements are displayed in my grid, this is connected with the actual number of rows too.

  4. #4
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890
    I can not do this cast: ((DataTable)this.dataGrid1.DataSource), my datasource is a DataSet with two DataTable
    hmm, you need to cast in DataSet and then take out the datatable from dataset ? isn't it ...


    I would recommend use index instead of category name that would prevent from naming ..


    foreach(DataRow row in myList.Tables["myCategory"].Rows)
    instead you can write like this


    foreach(DataRow row in myList.Tables[0].Rows)

    count the tables if and then go with index..

    thanx
    Paresh

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

Featured