dataGrid中显示主从表(点击主表的行后显示从表)

 string constr = "server=(local);uid=sa;pwd=123456; database=信息中心科技资料管理数据库";
            SqlConnection con = new SqlConnection(constr);

            string  com1 = "select * from 资料信息表";
            string  com2 = "select * from 资料借阅表";

            SqlDataAdapter ada1 = new SqlDataAdapter(com1,con);

            SqlDataAdapter ada2 = new SqlDataAdapter(com2,con);

            DataSet ds = new DataSet();

            ada1.Fill(ds, "资料信息表");
            ada2.Fill(ds, "资料借阅表");
            ds.Relations.Add("rel", ds.Tables["资料信息表"].Columns["档号"], ds.Tables["资料借阅表"].Columns["所借阅资料的档号"]);                             //定义关系后才能显示

            
            DataGrid datagrid = new DataGrid();
        
            datagrid.Location = new System.Drawing.Point(0, 0);
            datagrid.Size = new System.Drawing.Size(1000, 1000);
            datagrid.SetDataBinding(ds, "资料信息表");
          
            this.Controls.Add(datagrid);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WPF,如果需要通过点击头选DataGrid的一整列,可以按照以下步骤进行: 1. 在XAMLDataGrid添加一个事件处理程序 ```xaml <DataGrid x:Name="myDataGrid" AutoGenerateColumns="False" SelectionUnit="FullRow" SelectionChanged="myDataGrid_SelectionChanged" MouseLeftButtonUp="myDataGrid_MouseLeftButtonUp"> ``` 其,SelectionUnit属性设置为FullRow,示选整行。SelectionChanged事件会在选行发生改变时触发。MouseLeftButtonUp事件会在鼠标左键抬起时触发。 2. 在事件处理程序判断是否点击头 ```csharp private void myDataGrid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { // 判断是否点击头 DependencyObject obj = e.OriginalSource as DependencyObject; while (obj != null && obj != myDataGrid) { if (obj.GetType() == typeof(DataGridColumnHeader)) { // 获取列头所在的列 DataGridColumnHeader columnHeader = obj as DataGridColumnHeader; DataGridColumn column = columnHeader.Column; // 选整列 if (column is DataGridBoundColumn) { string bindingPath = (column as DataGridBoundColumn).Binding.Path.Path; foreach (var item in myDataGrid.Items) { DataGridRow row = myDataGrid.ItemContainerGenerator.ContainerFromItem(item) as DataGridRow; if (row != null) { DataGridCell cell = row.GetCellContent(column.DisplayIndex) as DataGridCell; if (cell != null && cell.DataContext != null) { PropertyInfo pi = cell.DataContext.GetType().GetProperty(bindingPath); if (pi != null) { object value = pi.GetValue(cell.DataContext, null); if (value != null && value.Equals(true)) { row.IsSelected = true; } else { row.IsSelected = false; } } } } } } break; } obj = VisualTreeHelper.GetParent(obj); } } ``` 其,判断是否点击头的代码可以根据实际需求进行修改。以上代码会在点击头时自动选整列。 希望能对你有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值