Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In Text File so many Barcodes and other data is there, i split all the Barcodes in separate text file, now how can i Bind that codes in DataGrid in C# ?
C#
TextWriter sw = new StreamWriter("Path");
DirectoryInfo dirinfo = new DirectoryInfo(txtSelFile.Text);
string[] lines = System.IO.File.ReadAllLines(txtSelFile.Text);
int count = File.ReadLines(txtSelFile.Text).Count();
MessageBox.Show("The total Barcodes is"+count);
String BC = string.Empty;
string PrevBcode="";
foreach (string s in lines)
{
    string[] fname1 = s.Split('|');                            
    if (fname1[0] == "AAA")
    {
        BC = fname1[2];
                       
        if (fname1[2] != PrevBcode)
        {
            PrevBcode = fname1[2];
            sw.Write(BC);
            sw.WriteLine("");
        }              
    }                                   
} 
sw.Close();
Posted
Updated 11-Dec-14 21:06pm
v4
Comments
Marcin Kozub 12-Dec-14 2:42am    
Can you share some code? Text file format? What have you tried? We don't have access to your computer, hard drive. Update your question with more informations.
Tomas Takac 12-Dec-14 3:20am    
Add the barcodes to a list, then bind the list to the grid.
Marcin Kozub 12-Dec-14 3:24am    
1. Why are you saving barcodes to another file? Do you need another file in other format or you just want filter barcodes?
2. Can you post example few lines of input file?
3. Do you need to select only barcodes that are not duplicated? What for is PrevBCode variable?
4. And what for you are using DirectoryInfo?

1 solution

Hi,

You don't have to save values to another file. You can load them to collection (like List) in memory and then bind to DataGridView. Here you have some sample:

C#
// Always check if file Exists
if (!File.Exists(txtSelFile.Text))
{
    throw new FileNotFoundException();
}

string[] lines = File.ReadAllLines(txtSelFile.Text);
string BC = string.Empty;
string PrevBcode = string.Empty;

// Create list of string type and store barcodes loaded from file.
List<string> barcodes = new List<string>();
foreach(string s in lines)
{
    string[] fname1 = s.Split('|');
    if (fname1[0] == "AAA")
    {
        BC = fname1[2];

        if (BC != PrevBcode)
        {
            PrevBcode = fname1[2];
            barcodes.Add(BC);
        }
    }
}

// Now we have loaded data in our LIST so we can bind it DataGridView
// You can't bing List<string> to datagridview because List<string> has no property to Bind.
// So we can create anonynous type:
dataGridView1.DataSource = barcodes.Select(x => new { Value = x }).ToList();</string></string></string></string>


Another solution is to create class for storing string values like this one (save it in StringValueClass.cs file in your project:
C#
public class StringValueClass
{
    public string StringValue { get; set; }

    public StringValueClass(string value)
    {
        StringValue = value;
    }
}


And modify your data loading code:
C#
List<StringValueClass> barcodes = new List<StringValueClass>();
foreach (string s in lines)
{
    string[] fname1 = s.Split('|');
    if (fname1[0] == "AAA")
    {
        BC = fname1[2];

        if (BC != PrevBcode)
        {
            PrevBcode = fname1[2];
            barcodes.Add(new StringValueClass(BC));
        }
    }
}
dataGridView1.DataSource = barcodes;


If you added column in DataGridView component, make sure that column has set DataPropertyName set to: StringValue

Good start for learning about collections is MSDN:
http://msdn.microsoft.com/en-us/library/system.collections.generic%28v=vs.110%29.aspx[^]

Instead of using textbox control to get file path you may consider using OpenFileDialog:
http://msdn.microsoft.com/en-us/library/system.windows.forms.openfiledialog%28v=vs.110%29.aspx[^]

I hope you find this useful.
 
Share this answer
 
v2

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