|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
DataGrid -> DataTable mapping problem[ Using C# in .Net 1.1 ] I'm trying ( amongst other things ) to create a per-row tooltip that displays all of the columns ( and cell value ) for that row - i need it as there are a large number of columns that will exceed the width of the app and the tooltip helps prevent the user from having to repeatedly scroll left and right. The following piece of code works prefect, except when the row order of the DataGrid doesnt match the DataTable ( hence after sorting / filtering etc.. ). I've seen answers similar to this problem, whereby you can use the CurrencyManager, however, that only seems to be able to deal with the currently selected row, or change the position ( forwards / backwards ). I want to get data from the datatable regardless of its position or whether it is selected or not. Any ideas ?! thanks, Rich. protected override void OnMouseMove(MouseEventArgs e) { HitTestInfo hi = this.HitTest( e.X, e.Y ); if ( hi.Type != HitTestType.ColumnHeader && -1 != hi.Row ) { if ( !( hi.Row == this.iHitRow && hi.Column == this.iHitCol ) ) { if( null != this.toolTip && this.toolTip.Active ) { this.toolTip.Active = false; } this.toolTip.SetToolTip( this, GenerateToolTip( hi.Row ) ); this.toolTip.Active = true; } } else { this.toolTip.SetToolTip( this, "" ); } this.iHitRow = hi.Row; this.iHitCol = hi.Column; base.OnMouseMove (e); } private string GenerateToolTip( int _iRow ) { string strToolTipText = ""; DataTable dataTable = (DataTable)this.DataSource; DataRow dataRow = dataTable.Rows[_iRow]; DataGridTableStyle dataGridTableStyle = this.TableStyles[0]; for( int iColumnIndex = 0; iColumnIndex < dataGridTableStyle.GridColumnStyles.Count; ++iColumnIndex ) { DataGridColumnStyle dataGridColumnStyle = dataGridTableStyle.GridColumnStyles[iColumnIndex]; // Ignore Columns with Width of 0 as these are 'hidden' if ( 0 < dataGridColumnStyle.Width ) { if ( 0 < strToolTipText.Length ) { strToolTipText += Environment.NewLine; } strToolTipText += dataGridColumnStyle.HeaderText + ": " + dataRow[dataGridColumnStyle.MappingName]; } } return strToolTipText; } RichS,
Luckily you are in the ADONET newsgroup and not C#. I made once a sample for this in VB (pure a sample and very roughly). It uses the normal events from the datagridtextboxcolumns. It should not be that difficult to set it in C#. Some from the major differences if you dont know them from C# and VBNet in this sample The directcast is an exact cast in VBNet. It will be in C# by instance column = (DatagridTextBoxColumn) ts.GridColumnStyles[0]; For the withEvents you can set a handler to the dtbCol1 by instance dtbCo1.MouseEnter += new System.EventHandler(dtb_Col1_Tooltip); \\\ Private WithEvents dtbCol1 As DataGridTextBox Private ToolTip1 As New ToolTip ') Private Sub Form1_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles MyBase.Load Datagrid1.ReadOnly = True Dim ts As New DataGridTableStyle ts.MappingName = ds.Tables(0).TableName Dim column As New DataGridTextBoxColumn ts.GridColumnStyles.Add(column) DataGrid1.TableStyles.Add(ts) column = DirectCast(ts.GridColumnStyles(0), DataGridTextBoxColumn) dtbCol1 = DirectCast(column.TextBox, DataGridTextBox) column.MappingName = ds.Tables(0).Columns(0).ColumnName column.HeaderText = "Cor" column.Width = 30 dv = New DataView(ds.Tables(0)) dv.AllowNew = False DataGrid1.DataSource = dv End Sub Private Sub dtbCol1_ToolTip(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles dtbCol1.MouseEnter ToolTip1.SetToolTip(DirectCast(sender, DataGridTextBox), _ "Row: " & DataGrid1.CurrentRowIndex + 1) End Sub //// I hope this helps a little bit? Cor Thanks for the reply - forgive me if I've mis-read your code - but dont think
it helps with my problem. You are getting the data from the datagrid, i need to get the data from the datatable, but when the datagrid shows data in a different order to the datatable. Any other ideas ?! R. Show quoteHide quote "Cor Ligthert" wrote: > RichS, > > Luckily you are in the ADONET newsgroup and not C#. I made once a sample for > this in VB (pure a sample and very roughly). It uses the normal events from > the datagridtextboxcolumns. It should not be that difficult to set it in C#. > > Some from the major differences if you dont know them from C# and VBNet in > this sample > > The directcast is an exact cast in VBNet. It will be in C# by instance > column = (DatagridTextBoxColumn) ts.GridColumnStyles[0]; > For the withEvents you can set a handler to the dtbCol1 by instance > dtbCo1.MouseEnter += new System.EventHandler(dtb_Col1_Tooltip); > > \\\ > Private WithEvents dtbCol1 As DataGridTextBox > Private ToolTip1 As New ToolTip > ') > Private Sub Form1_Load(ByVal sender As Object, _ > ByVal e As System.EventArgs) Handles MyBase.Load > Datagrid1.ReadOnly = True > Dim ts As New DataGridTableStyle > ts.MappingName = ds.Tables(0).TableName > Dim column As New DataGridTextBoxColumn > ts.GridColumnStyles.Add(column) > DataGrid1.TableStyles.Add(ts) > column = DirectCast(ts.GridColumnStyles(0), DataGridTextBoxColumn) > dtbCol1 = DirectCast(column.TextBox, DataGridTextBox) > column.MappingName = ds.Tables(0).Columns(0).ColumnName > column.HeaderText = "Cor" > column.Width = 30 > dv = New DataView(ds.Tables(0)) > dv.AllowNew = False > DataGrid1.DataSource = dv > End Sub > Private Sub dtbCol1_ToolTip(ByVal sender As Object, _ > ByVal e As System.EventArgs) Handles dtbCol1.MouseEnter > ToolTip1.SetToolTip(DirectCast(sender, DataGridTextBox), _ > "Row: " & DataGrid1.CurrentRowIndex + 1) > End Sub > //// > > I hope this helps a little bit? > > Cor > > > Rich,
I think you are right, however I made it in C# for the ones who search this newsgroup. (be aware that there can be a little wait time for the tooltip). \\\Needs a datagrid on a form and an event to the formload //using System.Data; ToolTip ToolTip1 = new ToolTip(); private void dtbColTooltip(object sender, System.EventArgs e) { int rownumber = dataGrid1.CurrentRowIndex; ToolTip1.SetToolTip((DataGridTextBox) sender, ((DataView) dataGrid1.DataSource)[rownumber]["Text"].ToString()); } private void Form11_Load(object sender, System.EventArgs e) { DataTable dt = new DataTable(); dt.Columns.Add("WhatEver"); dt.Columns.Add("Text"); dt.LoadDataRow(new object[] {"Cor","Lives in Holland"},true); dt.LoadDataRow(new object[] {"Rich","Lives somewhere else"},true); dataGrid1.ReadOnly = true; DataGridTableStyle ts = new DataGridTableStyle(); ts.MappingName = dt.TableName; DataGridTextBoxColumn column = new DataGridTextBoxColumn(); ts.GridColumnStyles.Add(column); dataGrid1.TableStyles.Add(ts); column = (DataGridTextBoxColumn) ts.GridColumnStyles[0] ; DataGridTextBox dtbCol = (DataGridTextBox) column.TextBox; dtbCol.MouseEnter += new System.EventHandler(this.dtbColTooltip); column.MappingName = dt.Columns[0].ColumnName; column.HeaderText = "Names"; column.Width = 50; dt.DefaultView.AllowNew = false; dataGrid1.DataSource = dt.DefaultView; } I hope this helps those little bit? Cor Rich
Before you misunderstand, you are right that you in my opinion misreaded my code. :-) CorFinally...... fixed it.....
The solution in the end was blindingly simple. Given a row index from the DataGrid ( obtained via HitTesting from the MouseMove event args ) the following bit of code works great. public DataRow GetDataGridRow( int _iRow ) { if ( 0 <= _iRow && _iRow < this.CurrentView.Count ) { DataRowView xDRV = this.CurrentView[ _iRow ]; DataRow dataRow = xDRV.Row as DataRow; return dataRow; } return null; } private string GenerateToolTip( int _iRow ) { string strToolTipText = ""; DataRow dataRow = GetDataGridRow( _iRow ); if ( null != dataRow ) { DataGridTableStyle dataGridTableStyle = this.TableStyles[0]; for( int iColumnIndex = 0; iColumnIndex < dataGridTableStyle.GridColumnStyles.Count; ++iColumnIndex ) { DataGridColumnStyle dataGridColumnStyle = dataGridTableStyle.GridColumnStyles[iColumnIndex]; // Ignore Columns with Width of 0 as these are 'hidden' if ( 0 < dataGridColumnStyle.Width ) { if ( 0 < strToolTipText.Length ) { strToolTipText += Environment.NewLine; } strToolTipText += dataGridColumnStyle.HeaderText + ": " + dataRow[dataGridColumnStyle.MappingName]; } } } return strToolTipText; } Show quoteHide quote "Cor Ligthert" wrote: > Rich > > Before you misunderstand, you are right that you in my opinion misreaded my > code. > > :-) > > Cor > > > Rich,
I did not use it because in some cricumstances (scrolling to bottom) the hittest is AFAIK not always precise, While in fact my code only exist from \\\ DataGridTextBox dtbCol = (DataGridTextBox) column.TextBox; dtbCol.MouseEnter += new System.EventHandler(this.dtbColTooltip); /// \\\\ private void dtbColTooltip(object sender, System.EventArgs e) { int rownumber = dataGrid1.CurrentRowIndex; ToolTip1.SetToolTip((DataGridTextBox) sender, ((DataView) dataGrid1.DataSource)[rownumber]["Text"].ToString()); } //// :-) Cor
Other interesting topics
Convertin dbnull to string
disconnected typed dataset DataAdapter Update Does Nothing ADO error "There is already an open DataReader associated with this Connection" How to look at parameter string SQLHelper.ExecuteReader - Connection Close Oracle Data Providers (Connection Pooling & Transactions) Simple asynchronous method Deleting from a dataview Making my own strongly typed dataset |
|||||||||||||||||||||||