Sreenath G

Sreenath G

  • NA
  • 233
  • 309.8k

Loadin a CSV file into a DataGrid in Silverlight 4 Application

Apr 13 2011 12:24 PM
         

I have tried this and I can get all the columns get loaded in datagrid but I cant see the data in rows. All the rows are shown empty. Am I doing anything wrong here..Please check out and help..

private void Button_Click(object sender, RoutedEventArgs e)

        {

            try

            {

                lFnLoadFileData();

            }

            catch (Exception)

            {

 

                throw;

            }

        }

 

 

 

        void lFnLoadFileData()

        {

            try

            {

                OpenFileDialog lObjFileDlge = new OpenFileDialog();

 

                lObjFileDlge.Filter = "CSV Files|*.csv";

                lObjFileDlge.FilterIndex = 1;

 

                lObjFileDlge.Multiselect = false;

 

                bool? lBlnUserclicked = lObjFileDlge.ShowDialog();

 

                if (lBlnUserclicked != null || lBlnUserclicked == true)

                {

                    FileStream lObjFileStream = lObjFileDlge.File.OpenRead();

 

                    StreamReader lObjStreamReader = new StreamReader(lObjFileStream);

                    lFnGenerateData(lObjStreamReader);

 

                    lObjFileStream.Close();

 

                }

            }

            catch (Exception)

            {

 

                throw;

            }

        }

 

 

        void lFnGenerateData(StreamReader aReader)

        {

            try

            {

                bool lBlnIsColumns = true;

                string[] lArrCols = null;

 

                Dictionary<string, object> lObjDicData = null;

 

                ObservableCollection<Dictionary<string, object>> lArrDGRows = new ObservableCollection<Dictionary<string, object>>();

 

                dgrex.Columns.Clear();

 

                while (aReader.Read() != null)

                {

                    string lStrLine = aReader.ReadLine();

 

                    if (lStrLine == null)

                        break;

 

                    if (lStrLine.Trim() == "")

                        continue;

 

                    string[] lArrStrCells = null;

 

                    lArrStrCells = lStrLine.Split(",".ToCharArray());

 

                    if (lArrStrCells == null)

                        continue;

 

                    if (lBlnIsColumns)

                    {

                        lArrCols = lArrStrCells;

                        foreach (string lStrCell in lArrStrCells)

                        {

                            DataGridTextColumn lDGCol = new DataGridTextColumn();

                            lDGCol.Header = lStrCell;

                            lDGCol.Binding = new Binding(lStrCell);

                            dgrex.Columns.Add(lDGCol);

                        }

                        lBlnIsColumns = false;

                        continue;

                    }

 

                    if (lArrCols == null)

                        continue;

 

                    int lIntColID = 0;

 

                    lObjDicData = new Dictionary<string, object>();

 

                    foreach (string lStrCell in lArrStrCells)

                    {

                        lObjDicData[lArrCols[lIntColID]] = lStrCell.Trim();

                        lIntColID++;

                    }

 

                    lArrDGRows.Add(lObjDicData);

 

                }

 

                aReader.Close();

                dgrex.ItemsSource = lArrDGRows;

               

            }

            catch (Exception)

            {

 

                throw;

            }

        }

 

 

Thanks

Sreenath


Answers (1)