WPF的DataGrid自动绑定数据库时最后的空列到底要怎么消除
2016-03-04
展开全部
将AutoGenerateColumns设置为false。然后在代码中手动生成不同表相对应的百分比列宽的与数据库表的列名对应的列。其余部分不变即可。
代码:
string attr = "";
dataDataGrid.AutoGenerateColumns = false;
MultiSecurity ms = new MultiSecurity();
OracleConnection oraCon = ms.CreateConnection("msdb", "maple", "manager");
foreach (string att in ms.getAttributes(tablename, oraCon)) //获取指定表的所有列
{
attr += att + ",";
dataDataGrid.Columns.Add(new DataGridTextColumn() { Header = att, Binding = new Binding(att), Width = new DataGridLength(2, DataGridLengthUnitType.Star) }); //为DataGrid生成百分比列宽的列,相当于xaml中设置Width = "2*"
}
attr = attr.Substring(0, attr.Length - 1);
sql = "select "+attr+" from " + node.Name; //生成查询指定列的sql语句
DataTable dt = ms.getData(sql, oraCon);
dataDataGrid.ItemsSource = dt.DefaultView; //将结果集绑定到DataGrid
代码:
string attr = "";
dataDataGrid.AutoGenerateColumns = false;
MultiSecurity ms = new MultiSecurity();
OracleConnection oraCon = ms.CreateConnection("msdb", "maple", "manager");
foreach (string att in ms.getAttributes(tablename, oraCon)) //获取指定表的所有列
{
attr += att + ",";
dataDataGrid.Columns.Add(new DataGridTextColumn() { Header = att, Binding = new Binding(att), Width = new DataGridLength(2, DataGridLengthUnitType.Star) }); //为DataGrid生成百分比列宽的列,相当于xaml中设置Width = "2*"
}
attr = attr.Substring(0, attr.Length - 1);
sql = "select "+attr+" from " + node.Name; //生成查询指定列的sql语句
DataTable dt = ms.getData(sql, oraCon);
dataDataGrid.ItemsSource = dt.DefaultView; //将结果集绑定到DataGrid
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询