wpf 获取mysql 数据库列名绑定datagrid列名_如何将Wpf DataGridColumn绑定到对象?

试试这个:

ItemsSource="{Binding ElementName=window1, Path=Users, Mode=TwoWay}" MouseDoubleClick="listViewUsers_MouseDoubleClick">

其中ItemsSource =“{Binding ElementName = window1,Path = Users,Mode = TwoWay}”

> ElementName是XAML中Window的名称(只需将x:Name =“window1”添加到Window标记中,就像使用任何其他ontrol一样.

>用户是List,应该与Dictionary一样

> Mode = TwoWay意味着如果网格被修改,列表也会被修改,反之亦然(双向绑定)

编辑:

试试这个:

XAML:

C#:

public class TheClass

{

public int Col1, Col2, Col3;

public Dictionary otherColumns = new Dictionary();

}

public class OtherColumns

{

public string ColumnName;

public int Value;

}

并在Window_Loaded下调用此方法:

private void PopulateListView()

{

TheClass c = new TheClass();

c.Col1 = 10;

c.Col2 = 20;

c.Col3 = 30;

c.otherColumns.Add(0, new OtherColumns() { ColumnName = "Col4", Value = 40 });

c.otherColumns.Add(1, new OtherColumns() { ColumnName = "Col5", Value = 50 });

c.otherColumns.Add(3, new OtherColumns() { ColumnName = "Col6", Value = 60 });

DataTable table = new DataTable();

// adding regular columns

table.Columns.Add("Col1", typeof(int));

table.Columns.Add("Col2", typeof(int));

table.Columns.Add("Col3", typeof(int));

// adding dynamic columns

foreach (KeyValuePair pair in c.otherColumns)

{

table.Columns.Add(pair.Value.ColumnName, typeof(int));

}

DataRow row = table.NewRow();

// adding regular column values to the DataTable

row["Col1"] = c.Col1;

row["Col2"] = c.Col2;

row["Col3"] = c.Col3;

// adding dynamic column values to the DataTable

foreach (KeyValuePair pair in c.otherColumns)

{

row[pair.Value.ColumnName] = pair.Value.Value;

}

table.Rows.Add(row);

// Start binding the table.

gridViewTest.Columns.Clear();

System.Windows.Controls.GridViewColumn gvc;

Binding binding;

foreach (DataColumn column in table.Columns)

{

gvc = new System.Windows.Controls.GridViewColumn();

binding = new System.Windows.Data.Binding();

binding.Path = new PropertyPath(column.ColumnName);

binding.Mode = BindingMode.OneWay;

gvc.Header = column.Caption;

gvc.DisplayMemberBinding = binding;

gridViewTest.Columns.Add(gvc);

}

listViewTest.DataContext = table;

}

我不是说这是最好的解决方案,但它可以提供帮助.让我知道.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值