This is a migrated thread and some comments may be shown as answers.

[Solved] Row Null problem

1 Answer 102 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
he seaman
Top achievements
Rank 1
he seaman asked on 11 Jan 2011, 08:16 AM
I have binding the gridview's itemsource, and it works right,but I need do some others work when i click the button,the problem is coming,
the gridview's row alway diaplay null??  why

 public partial class MainPage : UserControl
    {
        ObservableCollection<dynamic> obList = null;
       
        string sb = "";

        public MainPage()
        {
            InitializeComponent();
            WCFTestClient wcf = new WCFTestClient();
            wcf.GetDataAsync();
            wcf.GetDataCompleted += new EventHandler<GetDataCompletedEventArgs>(wcf_GetDataCompleted);
         
        }

         
           void wcf_GetDataCompleted(object sender, GetDataCompletedEventArgs e)
        {

            obList = DataTableHelp.getListByDataTableStr(e.Result);

            gridMain.ItemsSource = obList;
          
            for (int i = 0; i < gridMain.Columns.Count; i++)
            {
                System.Windows.Controls.ComboBoxItem comboItem = new ComboBoxItem();
                comboItem.Content = gridMain.Columns[i].UniqueName;

                col1.Items.Add(comboItem);
            }
         

       }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
              DataTable dt = new DataTable();
                     
              
            foreach (var column in gridMain.Columns)
            {
                GridViewDataColumn dataColumn = column as GridViewDataColumn;
                DataColumn dc = new DataColumn();
                if (dataColumn != null)
                {
                    dc.ColumnName = dataColumn.UniqueName;
                    dc.DataType = dataColumn.DataType;
                    dt.Columns.Add(dc);
                }
            }

            dt.Columns.Add(new DataColumn() { ColumnName = "RS", DataType = typeof(double) }); 
           
              foreach (var item in this.gridMain.Items)
            {

                string s = txt_rs.Text;
                GridViewRow row = this.gridMain.ItemContainerGenerator.ContainerFromItem(item) as GridViewRow;
                DataRow pr = new DataRow();
                if (row != null)
                {

                    foreach (GridViewCell cell in row.Cells)
                    {

                        if (cell.Column.UniqueName != "RS")
                        {
                            pr[cell.Column.UniqueName] = cell.Value;
                            string t = "";
                            if (cell.Value.ToString() == "" || cell.Value == null)
                            {
                                t = "(0)";
                            }
                            else
                            {
                                t = cell.Value.ToString().Trim();
                                if (t.First().ToString().Trim() == "-")
                                {
                                    t = "(0" + t + ")";
                                }

                            }
                            s = Replace(s, cell.Column.UniqueName, t);
                        }

                    }
                    pr["RS"] = this.cal(s);
                    dt.Rows.Add(pr);

               }

            }
          
                  
            gridMain.ItemsSource = dt.ToList();         

        }

the dt.rows.count is not same the gridMain.Items.count
pls help me ,thanks a lot

1 Answer, 1 is accepted

Sort by
0
Veselin Vasilev
Telerik team
answered on 11 Jan 2011, 09:23 AM
Hi he seaman,

This is a normal behavior because of the row virtualization. The grid.Items contains all the items in the grid (in or out of the viewport). The line:
GridViewRow row = this.gridMain.ItemContainerGenerator.ContainerFromItem(item) as GridViewRow;

will work only for the rows that are in the viewport. For all other items the row will be null.

You can try to switch off the row virtualization and see if it behaves the way you expect.

All the best,
Veselin Vasilev
the Telerik team
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
Tags
GridView
Asked by
he seaman
Top achievements
Rank 1
Answers by
Veselin Vasilev
Telerik team
Share this question
or