0

I am currently working on a C# WPF project where I am programmatically creating a Data set and binding this to a data grid.

The program is reading in a log file and processing each line of the log file into various columns within the data grid.

The data grid can have different types of columns depending on the type of log that is being viewed.

One of the columns is an error code, what I am trying to achieve is when the user hovers the mouse over the status code a tooltip will appear showing the meaning of the status code.

For example if the status code in the column was 404 the tooltip would show 'A 404 error means the page couldn't be found'

Below is the code for how I am creating the data set and binding to the datagrid

public void processAccessLog(System.Windows.Controls.UserControl userControl, System.Windows.Controls.DataGrid apacheDataGrid)
{
    string client;
    string remoteUser;
    string date;
    string requestLine;
    string statusCode;
    string clientSizeReturned;
    string referer;
    string userAgent;

    using (StreamReader reader = new StreamReader(ApacheFilePath))
    {
        string line;
        DataSet ds = new DataSet();
        DataTable table = new DataTable();
        DataColumn clientCol = new DataColumn("Client IP", typeof(string));
        DataColumn remoteUserCol = new DataColumn("Remote User", typeof(string));
        DataColumn dateCol = new DataColumn("Date", typeof(string));
        DataColumn requestLineCol = new DataColumn("Request Line", typeof(string));
        DataColumn statusCodeCol = new DataColumn("Status Code", typeof(string));
        DataColumn clientSizeReturnedCol = new DataColumn("Client Size Returned", typeof(string));
        DataColumn refererCol = new DataColumn("Referer", typeof(string));
        DataColumn userAgentCol = new DataColumn("User Agent", typeof(string));
        //DataColumn messageCol = new DataColumn("Message", typeof(string));

        table.Columns.Add(clientCol);
        table.Columns.Add(remoteUserCol);
        table.Columns.Add(dateCol);
        table.Columns.Add(requestLineCol);
        table.Columns.Add(statusCodeCol);
        table.Columns.Add(clientSizeReturnedCol);
        table.Columns.Add(refererCol);
        table.Columns.Add(userAgentCol);
        ds.Tables.Add(table);

        while ((line = reader.ReadLine()) != null)
        {
            client = line.Substring(0, line.IndexOf(' '));
            int startOfRemoteUser = client.Length + 1;
            remoteUser = line.Substring(startOfRemoteUser, CommonTasks.getIndexOfSpaceCharAfterIndex(startOfRemoteUser, line) - client.Length);
            int startOfDate = CommonTasks.getIndexOfCharacterAfterStartPosition(startOfRemoteUser, '[', line) + 1;
            date = line.Substring(startOfDate, CommonTasks.getIndexOfCharacterAfterStartPosition(startOfDate, ']', line) - startOfDate);
            int startOfRequestLine = CommonTasks.getIndexOfCharacterAfterStartPosition(startOfDate, '"', line) + 1;
            requestLine = line.Substring(startOfRequestLine, CommonTasks.getIndexOfCharacterAfterStartPosition(startOfRequestLine, '"', line) - startOfRequestLine);
            int startOfStatusCode = line.IndexOf(' ', startOfRequestLine + requestLine.Length) + 1;
            statusCode = line.Substring(startOfStatusCode, line.IndexOf(' ', startOfStatusCode) - startOfStatusCode);
            int startOfSizeReturned = line.IndexOf(' ', startOfStatusCode + statusCode.Length) + 1;
            clientSizeReturned = line.Substring(startOfSizeReturned, line.IndexOf(' ', startOfSizeReturned) - startOfSizeReturned);
            int startOfReferer = CommonTasks.getIndexOfCharacterAfterStartPosition(startOfSizeReturned + clientSizeReturned.Length, '"', line) + 1;
            referer = line.Substring(startOfReferer, CommonTasks.getIndexOfCharacterAfterStartPosition(startOfReferer, '"', line)- startOfReferer);
            int startOfUserAgent = CommonTasks.getIndexOfCharacterAfterStartPosition(startOfReferer + referer.Length + 1, '"', line) + 1;
            userAgent = line.Substring(startOfUserAgent).Replace("\"", "");
            DataRow row = table.NewRow();
            row[clientCol] = client;
            row[remoteUserCol] = remoteUser;
            row[dateCol] = date;
            row[requestLineCol] = requestLine;
            row[statusCodeCol] = statusCode;
            row[clientSizeReturnedCol] = clientSizeReturned;
            row[refererCol] = referer;
            row[userAgentCol] = userAgent;
            table.Rows.Add(row);
        }
        userControl.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background, new System.Windows.Threading.DispatcherOperationCallback(delegate
        {
            apacheDataGrid.ItemsSource = ds.Tables[0].DefaultView;
            return null;
        }), null);  
    }

Thanks for any help you can provide

Boardy
  • 35,417
  • 104
  • 256
  • 447
  • maybe the code at http://stackoverflow.com/questions/18699717/setting-tooltip-for-datagridview-automatically-created-columns might help? – pennstatephil Apr 10 '14 at 21:27
  • or alternatively http://www.codeproject.com/Articles/12402/Datagrid-Cell-Tooltip – pennstatephil Apr 10 '14 at 21:30
  • They're both close to what I'm after, except the first one is every row has the same tooltip for a column, and the second one is web related. I need the tooltip to be only visible on one column and the tooltip should be different for each status code that is shown – Boardy Apr 10 '14 at 21:55
  • couldn't you modify the first one by nesting a switch inside to say (pseudocode obviously) "`if` (this is the column I want to put tooltips on), `switch`(the status code)" and have case for each status (404, etc)... – pennstatephil Apr 10 '14 at 21:58
  • As far as I can see, the event handler only gets called once when the column is added, so I can only check on the column header, I can't check on the cell value in this event handler as this is called before the data is added only the columns – Boardy Apr 10 '14 at 22:01
  • according to this, you can use `RowDataBound` after the row data is bound: http://stackoverflow.com/questions/7303982/how-to-bind-tooltip-on-datagrid-rows – pennstatephil Apr 10 '14 at 22:05
  • RowDataBound is also part of the web namespace – Boardy Apr 10 '14 at 22:28
  • oh wow I'm sorry I didn't notice it was wpf instead of web. – pennstatephil Apr 10 '14 at 22:29
  • just found this for wpf-- looks like you can set tooltips on columns and again, maybe a switch on text value: http://www.c-sharpcorner.com/uploadfile/dpatra/tooltip-in-datagrid-in-wpf/ – pennstatephil Apr 10 '14 at 22:35
  • A really horrible way of using WPF, have you heard of XAML, data templates, styles ... and tooltips ?? http://msdn.microsoft.com/en-us/library/system.windows.controls.tooltip.aspx – aybe Apr 10 '14 at 23:52

0 Answers0