Click here to Skip to main content
14,865,273 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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
Posted
Updated 13-Apr-11 6:37am
v2

1 solution

Hi

Can someone please suggest me books or eBooks on internet on Silver Light designing and coding for a beginner.
Payroll Software
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900