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 a ArrayList, ‘MyCollection[]’ for an array of MyCollection objects, or ‘MyTableName’ for a datatable, or whatever.
Here is a code snippet provide by NoiseEHC on the microsoft.public.dotnet.framework.windowsforms.controls newsgroup that you can use to see exactly what mappingname is required for your datasource.
[C#]
//usage
ShowMappingName(dataGrid1.DataSource);
//implementation
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);
}
[VB.NET]
Private Sub ShowMappingName(ByVal src As Object)
Dim list As IList = Nothing
Dim t As Type = Nothing
If TypeOf (src) Is Array Then
t = 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
t = 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(t.Name)
End If
End Sub
Share with