gpt4 book ai didi

c# - 在wpf中动态添加列到DataGrid

转载 作者:太空狗 更新时间:2023-10-30 01:06:23 26 4
gpt4 key购买 nike

我目前正在自定义 Canvas 上工作,并且我必须添加一个表格,所以我认为 dataGrid 会很好。所以我想从“Datagrid”创建一个“Table”,用户可以通过它在运行时将表格添加到 Canvas 。

到目前为止,我已经尝试使用列表填充 DataGrid 并成功了。

如何在运行时将列添加到数据网格,以便在运行时使用文本框从用户那里获取列数和标题值,并且数据网格应根据文本框的值添加列和标题值.

实际上我想开发一个表,用户在其中传递列号和列标题,然后应该生成表。

或者

“您能否建议我使用 DrawingVisual 类“绘制”表格的方法”

它是 GraphicsTable 类的一部分

//Custom Classes "DrawingCanvas & GraphicsTable" 
public void CreateDataGrid(GraphicsTable graphicsTable, DrawingCanvas drawingCanvas)
{
dt = new DataGrid();
dt.Name = "Data";
dt.ItemsSource = person();
dt.AllowDrop = true;
dt.AutoGenerateColumns = true;
dt.Height = graphicsTable.Rectangle.Height;
dt.Width = graphicsTable.Rectangle.Width;
drawingCanvas.Children.Add(dt);
Canvas.SetTop(dt, graphicsTable.Rectangle.Top);
Canvas.SetLeft(dt, graphicsTable.Rectangle.Left);
dt.Width = dt.Width;
dt.Height = dt.Height;
dt.Focus();
}
//I have just tried to add dome dummy data to the datagrid.

public List<Person> person()
{
List<Person> peep = new List<Person>();
peep.Add(new Person() {});
return peep;
}

public class Person
{
private string name;
private double salary;
public string Names
{
get { return name; }
set { name = value; }
}
public double Salary
{
get { return salary; }
set { salary = value; }
}
}

最佳答案

您可以按如下方式动态构建 DataGrid 的列。

public void buildTable(string[] headers)
{
myGrid.Columns.Clear();
foreach (string header in headers)
{
DataGridTextColumn c = new DataGridTextColumn();
c.Header = header;
myGrid.Columns.Add(c);
}
}

但是,如果您正在设置 ItemsSource,行数和列数将自动调整以匹配 ItemsSource 的值。例如,以下代码生成具有 3 行和 3 列的 DataGrid。

dt = new DataTable();

for (int i = 0; i < 3; i++)
dt.Columns.Add("col" + i.ToString());

for (int i = 0; i < 3; i++)
{
DataRow r = items.NewRow();
r[0] = "a" + i.ToString();
r[1] = "b" + i.ToString();
r[2] = "c" + i.ToString();
dt.Rows.Add(r);
}

myGrid.ItemsSource = dt;
+------+------+------+  | col0 | col1 | col2 |  +------+------+------+  |  a0  |  b0  |  c0  |  +------+------+------+  |  a1  |  b1  |  c1  |  +------+------+------+  |  a2  |  b2  |  c2  |   +------+------+------+ 

Without knowing your exact requirements, I would not bother with manually drawing a table in code unless you have some special need custom graphics and even in that case I would look into using XAML to restyle the DataGrid or it's elements before attempting to render it myself. That's just my opinion though. Best of luck!

EDIT:

If you want to generate the table columns based on user input, you would just need to put the column generation code in a event handler. In your example you could add an event handler for the Textbox TextChanged event as follows. This event handler will run every time the text changes in the Textbox. You may want to add validation to prevent users from keying in large numbers.

private void numColsTextbox_TextChanged(object sender, TextChangedEventArgs e)
{
int numCols;
if (Int32.TryParse(tb.Text, out numCols))
{
myGrid.Columns.Clear();
for (int i = 1; i <= numCols; i++)
{
DataGridTextColumn c = new DataGridTextColumn();
c.Header = "Column " + i.ToString();
myGrid.Columns.Add(c);
}
}
}

关于c# - 在wpf中动态添加列到DataGrid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15655271/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com