Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
void lFnGenerateData(StreamReader aReader)
{
  try
  {
    bool lBlnIsColumns = true;
    string[] lArrCols = null;
    List<personalinfo> lstPersonalList = new List<personalinfo>();
    dataGrid1.Columns.Clear();
    while (aReader.Peek() &gt; 0)
    {
      string lStrLine = aReader.ReadLine();
      if (lStrLine == null)
        break;
      if (lStrLine.Trim() == "")
        continue;
      string[] lArrStrCells = null;
      lArrStrCells = lStrLine.Split(';');
      if (lArrStrCells == null)
        continue;
      if (lBlnIsColumns)
      {
        lArrCols = lArrStrCells;
        foreach (string lStrCell in lArrStrCells)
        {
          DataGridTextColumn lDGCol = new DataGridTextColumn();
          lDGCol.Header = lStrCell;
          lDGCol.Binding = new System.Windows.Data.Binding(lStrCell);
          dataGrid1.Columns.Add(lDGCol);
        }
        lBlnIsColumns = false;
        continue;
      }
      if (lArrCols == null)
        continue;
                   
      int lIntColID = 0;
      PersonalInfo objPersonalInfo = new PersonalInfo();
      objPersonalInfo.enumHeader = lArrStrCells[0];
      objPersonalInfo.FirstName = lArrStrCells[1];
      objPersonalInfo.LastName = lArrStrCells[2];
      objPersonalInfo.Address = lArrStrCells[3];
 
      lstPersonalList.Add(objPersonalInfo);
    }
    aReader.Close();
    dataGrid1.ItemsSource = lstPersonalList;
 
  }
  catch (Exception)
  {
    throw;
  }
}
Hello, please help me this above code which is to import CSV file into a WPF DataGrid. My question is, if i want to bind one column with Enum values, what should be here?
C#
PersonalInfo objPersonalInfo = new PersonalInfo(); objPersonalInfo.EnumColumn = lArrStrCells[0];
How to convert String to Enum.

The rest of my code is:
C#
private void _buttonImport_Click(object sender, RoutedEventArgs e)
        {

            try
            {
                lFnLoadFileData();
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show("Input string was not in correct format ,select other file");
            }


        }

        void lFnLoadFileData()
        {
            try
            {
                Microsoft.Win32.OpenFileDialog lObjFileDlge = new Microsoft.Win32.OpenFileDialog();
                lObjFileDlge.Filter = "CSV Files|*.csv";
                lObjFileDlge.FilterIndex = 1;
                lObjFileDlge.Multiselect = false;
                string fName = "";

                bool? lBlnUserclicked = lObjFileDlge.ShowDialog();
                if (lBlnUserclicked != null || lBlnUserclicked == true)
                {
                    fName = lObjFileDlge.FileName;
                }
                if (System.IO.File.Exists(fName) == true)
                {

                    StreamReader lObjStreamReader = new StreamReader(fName);

                    lFnGenerateData(lObjStreamReader);
                    lObjStreamReader.Close();
                }
            }
            catch (Exception)
            {
                throw;
            }

        }
        void lFnGenerateData(StreamReader aReader)
        {
            try
            {
                bool lBlnIsColumns = true;
                string[] lArrCols = null;
                List<Personal> lstPersonalList = new List<Personal>();
                dataGrid1.Columns.Clear();
                while (aReader.Peek() > 0)
                {
                    string lStrLine = aReader.ReadLine();
                    if (lStrLine == null)
                        break;
                    if (lStrLine.Trim() == "")
                        continue;
                    string[] lArrStrCells = null;
                    lArrStrCells = lStrLine.Split(';');
                    if (lArrStrCells == null)
                        continue;
                    if (lBlnIsColumns)
                    {
                        lArrCols = lArrStrCells;
                        foreach (string lStrCell in lArrStrCells)
                        {
                            DataGridTextColumn lDGCol = new DataGridTextColumn();
                            lDGCol.Header = lStrCell;
                            lDGCol.Binding = new System.Windows.Data.Binding(lStrCell);
                            dataGrid1.Columns.Add(lDGCol);
                        }
                        lBlnIsColumns = false;
                        continue;
                    }
                    if (lArrCols == null)
                        continue;

                    int lIntColID = 0;
                   PersonalobjPersonalInfo = new Personal();
                   objPersonalInfo.enumHeader = lArrStrCells[0];
objPersonalInfo.FirstName = lArrStrCells[1];
objPersonalInfo.LastName = lArrStrCells[2];
objPersonalInfo.Address = lArrStrCells[3];

                   
                  
                 

                    lstPersonalList.Add(objPersonalInfo);
                }
                aReader.Close();
                dataGrid1.ItemsSource = lstPersonalList;

            }
            catch (Exception)
            {
                throw;
            }
        }


public enum MyEnum{ one,two,three,four} Public Class Events { public MyEnum Event { get { return _event; } }
Posted
Updated 23-Aug-12 23:11pm
v2
Comments
Prabhakaran Soundarapandian 24-Aug-12 4:47am    
can you post your PersonalInfo DTO/class?
getanswer 24-Aug-12 5:01am    
private void _buttonImport_Click(object sender, RoutedEventArgs e)
{

try
{
lFnLoadFileData();
}
catch (Exception ex)
{
System.Windows.MessageBox.Show("Input string was not in correct format ,select other file");
}


}

void lFnLoadFileData()
{
try
{
Microsoft.Win32.OpenFileDialog lObjFileDlge = new Microsoft.Win32.OpenFileDialog();
lObjFileDlge.Filter = "CSV Files|*.csv";
lObjFileDlge.FilterIndex = 1;
lObjFileDlge.Multiselect = false;
string fName = "";

bool? lBlnUserclicked = lObjFileDlge.ShowDialog();
if (lBlnUserclicked != null || lBlnUserclicked == true)
{
fName = lObjFileDlge.FileName;
}
if (System.IO.File.Exists(fName) == true)
{

StreamReader lObjStreamReader = new StreamReader(fName);

lFnGenerateData(lObjStreamReader);
lObjStreamReader.Close();
}
}
catch (Exception)
{
throw;
}

}
void lFnGenerateData(StreamReader aReader)
{
try
{
bool lBlnIsColumns = true;
string[] lArrCols = null;
List<Personal> lstPersonalList = new List<Personal>();
dataGrid1.Columns.Clear();
while (aReader.Peek() > 0)
{
string lStrLine = aReader.ReadLine();
if (lStrLine == null)
break;
if (lStrLine.Trim() == "")
continue;
string[] lArrStrCells = null;
lArrStrCells = lStrLine.Split(';');
if (lArrStrCells == null)
continue;
if (lBlnIsColumns)
{
lArrCols = lArrStrCells;
foreach (string lStrCell in lArrStrCells)
{
DataGridTextColumn lDGCol = new DataGridTextColumn();
lDGCol.Header = lStrCell;
lDGCol.Binding = new System.Windows.Data.Binding(lStrCell);
dataGrid1.Columns.Add(lDGCol);
}
lBlnIsColumns = false;
continue;
}
if (lArrCols == null)
continue;

int lIntColID = 0;
PersonalobjPersonalInfo = new Personal();
objPersonalInfo.enumHeader = lArrStrCells[0];
objPersonalInfo.FirstName = lArrStrCells[1];
objPersonalInfo.LastName = lArrStrCells[2];
objPersonalInfo.Address = lArrStrCells[3];





lstPersonalList.Add(objPersonalInfo);
}
aReader.Close();
dataGrid1.ItemsSource = lstPersonalList;

}
catch (Exception)
{
throw;
}
}


public enum MyEnum{ one,two,three,four} Public Class Events { public MyEnum Event { get { return _event; } }
Prabhakaran Soundarapandian 24-Aug-12 5:09am    
Try my solution posted below...

you can use the Enum.Parse method;

EnumType enValue = (EnumType) Enum.Parse(typeof(EnumType), valueString);

Hope this helps :)

Jas
 
Share this answer
 
Let say your enum is

C#
enum EHeader
   {
      One,
      Two,
      Three
   }


C#
string lArrStrCells[0] = "One";

objPersonalInfo.enumHeader = (EHeader)Enum.Parse(typeof(EHeader), lArrStrCells[0] , true);

// Picking an invalid colour throws an ArgumentException. To
// avoid this, call Enum.IsDefined() first, as follows:

if (Enum.IsDefined(typeof(EHeader), lArrStrCells[0] ))
      objPersonalInfo.enumHeader = (EHeader)Enum.Parse(typeof(EHeader), lArrStrCells[0] , true);
 
Share this answer
 
v3
Comments
getanswer 24-Aug-12 5:19am    
Hi, i tried with this , Error is " Input string was not in correct format, select other file"
Prabhakaran Soundarapandian 24-Aug-12 5:20am    
show your piece of code where you are using it and the DTO and Enum

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