我目前正在使用自定义 Canvas ,因此我必须添加一个表格,所以我认为 dataGrid 会很好。所以我想从“Datagrid”创建一个“表”,用户可以通过它在运行时向 Canvas 添加一个表。
到目前为止,我已经尝试使用列表填充 DataGrid 并成功。
如何在运行时向 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/