WPF中使用datagrid显示数据库里面的内容
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF中使用datagrid显示数据库里面的内容相关的知识,希望对你有一定的参考价值。
代码如下: mysqlDataAdapter da = new MySqlDataAdapter("SELECT uid,uname,password FROM login", conn); System.Data.DataSet ds = new System.Data.DataSet(); da.Fill(ds); datagrid1.ItemsSource = ds.Tables[0].DefaultView;
显示:
如果在前台加上AutoGenerateColumns="False",那么显示的时候就只是
怎么才把后面多余的列去掉呢?
例如:
<DataGrid AutoGenerateColumns="False" Height="418" HorizontalAlignment="Left" Name="dataGrid1" VerticalAlignment="Top" Width="316" >
<DataGrid.Columns>
<DataGridTextColumn Header="列名" Binding="Binding Path=你的ds.Tables[0]的某一列的列名, Mode=TwoWay" Width="380"/>
</DataGrid.Columns>
</DataGrid>追问
这样弄了后,表格里就没有内容显示出来了,为什么呢?
参考技术B 这是一个小技巧啦。在Grid的最后一列,比如你图上的“password”列,将这个列的宽度不设定为固长,而是设为自适应宽度(*)就可以了。追问如果设置成 “*”了,前台AutoGenerateColumns="False"时,也没有内容显示出来;前台AutoGenerateColumns="True"时,就会成这样
晕。你首先得搞清楚自己是否要AutoGenerateColumns=ture。如果AutoGenerateColumns=ture,那么在前台就不需要设置DataGrid.Columns。数据源有什么前台就会显示什么。
如果你确定前台有针对性的显示哪些列,就应该在DataGrid.Columns一一写出来。并在最后的一个column里设置其宽度为自适应宽度。
wpf 当DataGrid列模版是ComboBox时,显示信息
? 实际工作中,有时DataGrid控件某一列显示数据是从Enum集合里面选择出来的,那这时候设置列模版为ComboBox就能满足需求。而关于显示的实际内容,直接是Enum的string()返回值可能不太适合,这时候采用System.ComponentModel.Description是一个很好用的方法。
代码中定义的显示类型是Enum,实际结果在Description中声明。
定义 Enum Week
[System.ComponentModel.Description("星期")]
public enum Week
[System.ComponentModel.Description("星期一")]
Monday,
[System.ComponentModel.Description("星期二")]
Tuesday,
[System.ComponentModel.Description("星期三")]
Wednesday,
[System.ComponentModel.Description("星期四")]
Thursday,
[System.ComponentModel.Description("星期五")]
Firday,
[System.ComponentModel.Description("星期六")]
Saturday,
[System.ComponentModel.Description("星期日")]
Sunday,
DataGrid模版:
<Grid.Resources>
<Style x:Key="DataGridTextColumnStyle" TargetType="x:Type TextBlock">
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
</Style>
<Style TargetType="x:Type DataGrid">
<Setter Property="Margin" Value="0"/>
<Setter Property="Background" Value="#FF8CEB87"/>
<Setter Property="AutoGenerateColumns" Value="False"/>
<Setter Property="CanUserAddRows" Value="False"/>
<Setter Property="CanUserReorderColumns" Value="False"/>
<Setter Property="CanUserSortColumns" Value="False"/>
<Setter Property="CanUserResizeColumns" Value="False"/>
<Setter Property="CanUserResizeRows" Value="False"/>
<Setter Property="RowHeaderWidth" Value="30"/>
<Setter Property="RowHeight" Value="30"/>
<Setter Property="IsReadOnly" Value="True"/>
<Setter Property="CellStyle">
<Setter.Value>
<Style TargetType="DataGridCell">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
</Setter.Value>
</Setter>
<Setter Property="ColumnHeaderStyle">
<Setter.Value>
<Style TargetType="DataGridColumnHeader">
<Setter Property="HorizontalContentAlignment" Value="Center"/>
</Style>
</Setter.Value>
</Setter>
</Style>
</Grid.Resources>
<DataGrid ItemsSource="Binding TestDatas" LoadingRow="DataGrid_LoadingRow" Margin="0,0,403.6,10">
<DataGrid.Resources>
<ObjectDataProvider x:Key="Weeks" MethodName="GetNames" ObjectType="x:Type sys:Enum">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="local:Week"/>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
<local:WeekEnumToDescriptionConvertor x:Key="WeekEnumToDescription"/>
<local:WeekEnumToComboBoxIndexConvertor x:Key="WeekEnumToComboBoxIndex"/>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTemplateColumn Header="Week" Width ="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="Binding Source=StaticResource Weeks, Converter=StaticResource WeekEnumToDescription"
SelectedIndex="Binding TestWeek, Converter=StaticResource WeekEnumToComboBoxIndex, UpdateSourceTrigger=PropertyChanged"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="Message"
Binding="Binding TestMsg"
IsReadOnly="True"
ElementStyle ="StaticResource DataGridTextColumnStyle"
Width ="*"/>
</DataGrid.Columns>
</DataGrid>
WeekEnumToDescriptionConvertor、WeekEnumToComboBoxIndexConvertor实现代码:
class WeekEnumToComboBoxIndexConvertor : System.Windows.Data.IValueConverter
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
return ((int)(Week)value);
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
return ((Week)(int)value);
class WeekEnumToDescriptionConvertor : System.Windows.Data.IValueConverter
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
var strValue = value as string[];
if (strValue != null)
var enValue = new Week[strValue.Length];
for (int i = 0; i < strValue.Length; i++)
if (Enum.TryParse(strValue[i], out enValue[i]))
strValue[i] = enValue[i].GetDescription();
return value;
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
throw new NotImplementedException();
EnumHelper代码:
public static class EnumHelper
public static string GetDescription<T>(this T value) where T : struct
string result = value.ToString();
var fi = typeof(T).GetField(result);
var attributes = (System.ComponentModel.DescriptionAttribute[])fi.GetCustomAttributes(
typeof(System.ComponentModel.DescriptionAttribute), true);
if (attributes != null && attributes.Length > 0)
return attributes[0].Description;
return result;
public static T GetValueByDescription<T>(this string description) where T : struct
Type type = typeof(T);
foreach (var field in type.GetFields())
if (field.Name == description)
return (T)field.GetValue(null);
var attributes = (System.ComponentModel.DescriptionAttribute[])field.GetCustomAttributes(
typeof(System.ComponentModel.DescriptionAttribute), true);
if (attributes != null && attributes.Length > 0)
if (attributes[0].Description == description)
return (T)field.GetValue(null);
throw new ArgumentException(string.Format($"description 未能找到对应的枚举"), "Description");
public static T GetValue<T>(this string value) where T : struct
T result;
if (Enum.TryParse(value, true, out result))
return result;
throw new ArgumentException(string.Format($"value 未能找到对应的枚举"), "Value");
最终效果图
以上是关于WPF中使用datagrid显示数据库里面的内容的主要内容,如果未能解决你的问题,请参考以下文章