ForEACH record in Datagrid

8 posts, 0 answers
  1. Nikhil
    Nikhil avatar
    23 posts
    Member since:
    May 2009

    Posted 22 Jun 2009 Link to this post

    Hi ,
    I am new to Silverlight + telerik and want to know a how can i traverse through each record in a sillverlight Telerik  Datagrid.

    What I am looking for is a Silverlight equivalent code for below AJax code


          foreach (GridDataItem Item  in SourceRadGrid.Items)
              {
                   if (((CheckBox)Item["TemplateColumnCheckBox"].FindControl("CheckBox1")).Checked)
                    {
                       
                        LineNo = Item.Cells[3].Text.ToString();
         
                    }

             }
    Experts please guide
  2. Casey
    Casey avatar
    27 posts
    Member since:
    May 2009

    Posted 22 Jun 2009 Link to this post

    I have the same question.

    I have a checkbox as the first column of each row and I want to implement a SelectAll and UnselectAll column.

    The column is not bound, so I think it makes sense to loop through and just check the items.

    casey
  3. Nikhil
    Nikhil avatar
    23 posts
    Member since:
    May 2009

    Posted 22 Jun 2009 Link to this post

    Checkbox is just an example i put,,,,but i really need to find a way ,,,,to travese through each record item in grid.. and get their contents
  4. Nikhil
    Nikhil avatar
    23 posts
    Member since:
    May 2009

    Posted 23 Jun 2009 Link to this post

    Can somebody please help? I's really urgent
  5. Casey
    Casey avatar
    27 posts
    Member since:
    May 2009

    Posted 24 Jun 2009 Link to this post

    It is disappointing that no one has responded...

    This gets the data out of the datacolumns.. but my checkbox column returns as nulll

     

     

    for (int ii = 0; ii < dataGrid.Items.Count; ii++)

     

    {

     

    var row = dataGrid.ItemsControl.ItemsGenerator.GenerateItemAtIndex(ii) as GridViewRow;

     

     

    object cellValue = null;

     

     

    for (int iCell = 0; iCell < dataGrid.Columns.Count; iCell++)

     

    {

    cellValue = row.Cells[iCell].Content;

     

    if (iCell == 0)

     

    {

     

    if (cellValue is CheckBox)

     

    {

     

    CheckBox c = (CheckBox)cellValue;

     

    c.IsChecked =

    true;

     

    }

     

    }

    }

    }

  6. Casey
    Casey avatar
    27 posts
    Member since:
    May 2009

    Posted 24 Jun 2009 Link to this post

    Got it.. Still don't understand why previous post returned null.

     

    for (int i = 0; i < dataGrid.Items.Count; i++)

     

    {

     

            var row = dataGrid.ItemsControl.ItemsGenerator.GenerateItemAtIndex(i) as GridViewRow;

     

     

            GridViewCell firstCell2 = row.Cells.Cast<GridViewCell>().FirstOrDefault();

     

     

            if (firstCell2 != null)

     

            {

                ((

    CheckBox)firstCell2.ChildrenOfType<CheckBox>().FirstOrDefault()).IsChecked=true;

     

            }

    }

  7. Nikhil
    Nikhil avatar
    23 posts
    Member since:
    May 2009

    Posted 25 Jun 2009 Link to this post

    Hi

    Casey

    I am still  getting NULL for the first column...i m not sure why it is working for you , Below in my code ,.,,,changes done in your code to implement radio button and Chckbox

    for (int i = 0; i < RadGridView1.Records.Count; i++)

     

    {

     

    var row = RadGridView1.ItemsControl.ItemsGenerator.GenerateItemAtIndex(i) as GridViewRow;

     

     

    GridViewCell firstCell2 = row.Cells.Cast<GridViewCell>().FirstOrDefault();

     

     

     

    if (firstCell2 != null)  

     

    {

    ((

    RadioButton)firstCell2.ChildrenOfType<RadioButton>().FirstOrDefault()).IsChecked = true;

     

     

    break;

     

    }

    }



  8. Kiran G
    Kiran G avatar
    1 posts
    Member since:
    Apr 2012

    Posted 04 Apr 2012 Link to this post

    Hi Frens,

    Use the following code to read the silverlight datagrid....



    foreach (DataGridRow row in GetDataGridRows(dgParams))
    {
       
    // parse your gird controls like below
       
    TextBlock txtParamName = dgParams.Columns[0].GetCellContent(row) as TextBlock;
       
    TextBox txtParamValue = dgParams.Columns[1].GetCellContent(row) as TextBox;
    }
    private List GetDataGridRows(DataGrid dataGrid)
    {
       
    return GetVisualChildCollection(dataGrid);
    }
    public static List GetVisualChildCollection(object parent) where T : FrameworkElement
    {
       
    List visualCollection = new List();
       
    GetVisualChildCollection(parent as DependencyObject, visualCollection);
       
    return visualCollection;
    }
    private static void GetVisualChildCollection(DependencyObject parent, List visualCollection) where T : FrameworkElement
    {
       
    int count = VisualTreeHelper.GetChildrenCount(parent);
       
    for (int i = 0; i < count; i++)
       
    {
           
    DependencyObject child = VisualTreeHelper.GetChild(parent, i);
           
    if (child is T)
           
    {
               
    visualCollection.Add(child as T);
            
    }
           
    else if (child != null)
           
    {
               
    GetVisualChildCollection(child, visualCollection);
           
    }
       
    }
    }



    *Note : it will read only visible grid controls.

Back to Top