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
Sam HobbsPosted Apr 13, 2011, 6:11 PM
What I would do is to determine if the problem is that the data is not being read or if the data is just not being put into the control properly. You need to learn how to use the debugger; I think you would quickly see a problem if you were to use the debugger. You can use the debugger to step through the code to see what happens in each line.
I see something that is probably one problem. You should try to use the debugger to learn how to find the problem. In your code is:
while (aReader.Read() != null)
Look at the documentation. The Read method will read a character. Try to use the debugger to see what happens when the program calls ReadLine. Look at the data that ReadLine gets.