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