Microsoft Communities

Welcome to WindowsClient.net | Sign in | Join

Here are some frequently asked questions about Windows Forms and their answers.

Windows Forms FAQs

If the user selects a row in a DataGrid, then scrolls it out of view with the mouse wheel, the user cannot get the selected row back into view. How do I work around this limitation?

When you select a row in the DataGrid and scroll it out of view using the mouse wheel, you cannot get it back into view. Here is a workaround.

dataGrid1.MouseWheel += new MouseEventHandler( dataGrid1_MouseWheel );

// ...

private void dataGrid1_MouseWheel( object sender, MouseEventArgs e )
{
  dataGrid1.Select();
}

Contributed from George Shepherd's Windows Forms FAQ

Q: How do I determine the DataGridTableStyle.MappingName that should used for a DataGrid to make sure the grid uses a custom table style?

The DataGrid looks for a DataGridTableStyle.MappingName that is the type name of its DataSource. So, depending upon what DataSource you are using, this may be "ArrayList" for an ArrayList, "SomeType[]" for an array of SomeType objects, or "MyTableName" for a DataTable.

Here is a code snippet that you can use to see exactly what MappingName is required for your DataSource.

[C#]

void ShowMappingName( object src )
{
  IList list = null;
  Type type = null;
  if ( src is Array )
  {
    type = src.GetType();
    list = src as IList;
  }
  else
  {
    if ( src is IListSource )
      src = (src as IListSource).GetList();
    if ( src is IList )
    {
      type = src.GetType();
      list = src as IList;
    }
    else
    {
      MessageBox.Show( "error" );
      return;
    }
  }
  if ( list is ITypedList )
    MessageBox.Show( (list as ITypedList).GetListName( null ) );
  else
    MessageBox.Show( type.Name );
}

[Visual Basic]

Private Sub ShowMappingName(ByVal src As Object)
  Dim list As IList = Nothing
  Dim type As Type = Nothing
  If TypeOf (src) Is Array Then
    type = src.GetType()
    list = CType(src, IList)
  Else
    If TypeOf src Is IListSource Then
      src = CType(src, IListSource).GetList()
    End If
    If TypeOf src Is IList Then
      type = src.GetType()
      list = CType(src, IList)
    Else
      MessageBox.Show("Error")
      Return
    End If
  End If
  If TypeOf list Is ITypedList Then
    MessageBox.Show(CType(list, ITypedList).GetListName(Nothing))
  Else
    MessageBox.Show(type.Name)
  End If
End Sub

The method would be used like this.

ShowMappingName( dataGrid1.DataSource );

George Shepherd, Syncfusion, and NoiseEHC

Q: How do I access other values in other columns of a DataGrid from the Paint override of a class derived from DataGridColumnStyle?

You can get a reference to the DataGrid like this.

Dim grid As DataGrid = DataGridTableStyle.DataGrid

Once you have the grid, you can use an indexer to get the value of any particular column on the same row.

Dim someValue as Object = grid(rowNum, 5) ' value in column 5....

Contributed from George Shepherd's Windows Forms FAQ



Page view counter