Holla, is possible to store log message in datagridview? or store log in file and after read text from file...

i like tohave DataGridView somehtink like

[Header] [B]User        Date                  ErrorType          Text[/B]
         Admin       22/9/2011 15:31 PM    LoginFail          Failed Login
         john        26/9/2011 05:33 PM    LoginSuccess       Login OK
         john        29/9/2011 22:20 PM    LoginSuccess       Login OK
         Admin       30/9/2011 11:33 PM    LoginFail          Failed Login
         System      30/9/2011 11:56 PM    WPageLoad          There was a error on welcome page loading.

But the only i know is to read all text from file or 1 line and put in DataGrid box, my idea is to create a local database file and store informations in DB, after load and show them in Datagridview....

smthing like this...
[IMG]http://img716.imageshack.us/img716/5126/74976555.png[/IMG]

If you intend to store the data in a database then you need to learn about reading/writing data to databases and how to use DataSet, DataTable, DataAdapter and SqlCommand objects.
There are many thread on this forum that show how to do this.

If you with to store the data in a file then you can use something like the code below to put the data in the data grid.

This is a quick example of using a list as the data source of a data grid.
The list could be filled by reading data stored in a file.

Support objects to hold the loaded log records.

/// <summary>
    /// Custom class for log message data
    /// </summary>
    public class LogData
    {
        public LogData(string user, DateTime date, string errorType, string text)
        {
            User = user;
            Date = date;
            ErrorType = errorType;
            Text = text;
        }

        public string User { get; set; }
        public DateTime Date { get; set; }
        public string ErrorType { get; set; }
        public string Text { get; set; }
    }

    /// <summary>
    /// Log message data collection
    /// </summary>
    public class LogDataSet : List<LogData> { }

Simple form with one button and basic datagrid added.

internal partial class Form4 : Form
    {
        public Form4()
        {
            InitializeComponent();
        }

        // collection object of messages to display in data grid
        private LogDataSet logmessages = new LogDataSet();

        private void button1_Click(object sender, EventArgs e)
        {
            // load data from log file
            GetDataFromFile();
            // (DataGrid uses reflection to get column names from LogData Class)
            dataGridView1.DataSource = logmessages;
        }

        private void GetDataFromFile()
        {
            // simulation of loading data
            for (int i = 0; i < 5; i++)
            {
                logmessages.Add(new LogData(string.Format("User{0}", i), DateTime.Now.AddMinutes(i), "LoginFail", "FailedLogin"));
            }
            logmessages.Add(new LogData("Me", DateTime.Now.AddMinutes(10), "CodeError", "Failed to supply good example"));
        }
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.