简体   繁体   English

在wpf中动态地将列添加到DataGrid

[英]Dynamically add Columns to DataGrid in wpf

I am currently working on a custom canvas and in that i have to add a table,So i thought dataGrid would be fine. 我目前正在制作一个自定义画布,并且我必须添加一个表,所以我认为dataGrid会很好。 SO i Want to create a " Table" from "Datagrid" by which user can add a table to the canvas at runtime. 所以我想从“Datagrid”创建一个“ 表” ,用户可以在运行时将一个表添加到画布。

Till now, I have tried to Populate DataGrid With a list and succeded. 直到现在,我已经尝试使用列表填充DataGrid并成功。

How Can I add Columns to a Datagrid at runtime,such that the number of columns and header value Would be taken from the user at runtime using a textbox and based on the value of the textbox the datagrid should add columns and header value. 如何在运行时将列添加到Datagrid,以便在运行时使用文本框从用户获取列数和标题值,并基于文本框的值,datagrid应添加列和标题值。

Actually I want to develop a Table in which user passes the no of columns and the column header and the table should be generated. 实际上我想开发一个表,其中用户传递no的列和列标题,并且应该生成表。

Or 要么

"Can you suggest me with a way where i should look in order to to "Draw" a Table using DrawingVisual class" “你能否建议我使用DrawingVisual类”绘制“表格”

It is a part of GraphicsTable Class 它是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; }
    }
}

You can dynamically build the columns of a DataGrid as follows. 您可以按如下方式动态构建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);
    }
}

If you are setting ItemsSource, however, the number of rows and columns will automatically adjust to match the value of ItemsSource. 但是,如果要设置ItemsSource,则行数和列数将自动调整以匹配ItemsSource的值。 For example, the following code produces a DataGrid with 3 rows and 3 columns. 例如,以下代码生成一个包含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. 在不知道您的确切要求的情况下,我不会费心手动在代码中绘制表格,除非您有一些特殊的需要自定义图形,即使在这种情况下,我会考虑使用XAML来重新设置DataGrid或它的元素,然后再尝试自己渲染它。 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. 在您的示例中,您可以为Textbox TextChanged事件添加事件处理程序,如下所示。 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);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM