I am new to window application.
I have written a code to read files inside a folder and read it line by line and find some string and display its count to dataGridView.
I wanted to fill a dataGridview which is not working.
I have tried different ways over the internet but none of them are working.
I know that I might be making a silly mistake.
My code is as follow
public class FileDetail
{
public string filename;
public int openConnectionCount;
public int closeConnectionCount;
}
private void button1_Click(object sender, EventArgs e)
{
int openConnectionCount = 0;
int closeConnectionCount = 0;
List<FileDetail> fileDetails =new List<FileDetail>();
foreach (string file in Directory.EnumerateFiles(textBox1.Text, "*.*", SearchOption.AllDirectories))
{
FileDetail fileDetail =new FileDetail();
fileDetail.filename = file;
// Read the file as one string.
using (StreamReader myFile = new StreamReader(file))
{
string line;
while ((line = myFile.ReadLine()) != null)
{
if (line.Contains(" OpenConnection()")) openConnectionCount++;
if (line.Contains(" CloseConnection()")) closeConnectionCount++;
}
fileDetail.openConnectionCount = openConnectionCount;
fileDetail.closeConnectionCount = closeConnectionCount;
}
fileDetails.Add(fileDetail);
openConnectionCount = 0;
closeConnectionCount = 0;
}
dataGridView1.AutoGenerateColumns = false;
dataGridView1.Columns.Add("filename", "File Name");
dataGridView1.Columns.Add("openConnectionCount", "Open Connection");
dataGridView1.Columns.Add("closeConnectionCount", "close Connection");
dataGridView1.Columns[0].DataPropertyName = "filename";
dataGridView1.Columns[1].DataPropertyName = "openConnectionCount";
dataGridView1.Columns[2].DataPropertyName = "closeConnectionCount";
var filenamesList = new BindingList<FileDetail>(fileDetails); // <-- BindingList
dataGridView1.DataSource = filenamesList;
//dataGridView1.DataSource = fileDetails;
//dataGridView1.b
//foreach (var fileDetail in fileDetails)
//{
// label1.Text += "FileName:=" + fileDetail.filename + "\t Open Connection:= " +
// fileDetail.openConnectionCount + "\t close connection:=" +
// fileDetail.closeConnectionCount+"\n";
//}
}
I have followed some link for the same
How to bind list to dataGridView?
http://social.msdn.microsoft.com/Forums/eu/csharplanguage/thread/766c7b46-6431-4ec1-8e74-50f718c6606b
Data binding only work with Set/Get Properties, not public field.
Try to change your FileDetail
like this :
public class FileDetail
{
public string filename { get; set; }
public int openConnectionCount { get; set; }
public int closeConnectionCount { get; set; }
}
if you want to auto generate columns of datagridview then you can use it as
public class FileDetail
{
[DisplayName("File Name")]
public string filename { get; set; }
[DisplayName("Open Connection")]
public int openConnectionCount { get; set; }
[DisplayName("close Connection")]
public int closeConnectionCount { get; set; }
}
more you can use List as Datasource without Binding source as
dataGridView1.DataSource = fileDetails.ToArray();
for more information you can check this link Bind List of Class objects as Datasource to DataGridView
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.