如何以编程方式将行添加到行尾

时间:2016-08-03 03:53:14

标签: c# wpf

我在WPF中使用datagrid,数据绑定成功,但我需要在行尾添加一个按钮。例如,如果status = 1,则添加红色按钮和“未确认”内容,如果状态为!= 1,则添加蓝色按钮,内容如“查看详细信息”

更新:

    if(status == "1")
                    {
                        //add a button to the end of row with red color and content 


                        foreach (DataTable dt in result.Tables)
                        {
                           tb.Columns.Add("ButtonColumn", typeof(Button));
                            foreach (DataRow row in tb.Rows)
                            {
                                r["ButtonColumn"] = new Button
                                {
                                    Name = "rowButton",
                                    Content = "Row Button Content",
                                    Width = 100,
                                    Height = 30
                                };
                            }
                        }
                        stock_details.ItemsSource = new DataView(result.Tables["STOCK_OFFICER"]);
                    }else if(status != "1")
                    {
                        //add a button to the end of row with blue color and content 
                        tb.Columns.Add("ButtonColumn", typeof(Button));

                        foreach (DataTable dtb in result.Tables)
                        {
                            foreach (DataRow ro in tb.Rows)
                            {
                                r["ButtonColumn"] = new Button
                                {
                                    Name = "rowButton",
                                    Content = "Second Button",
                                    Width = 100,
                                    Height = 30
                                };
                            }
                        }
                        stock_details.ItemsSource = new DataView(result.Tables["STOCK_OFFICER"]);
                    }

如何使用当前代码执行此操作?

1 个答案:

答案 0 :(得分:0)

试试这个。这是您正在寻找的代码。 为数据表中的按钮添加新列。使用每行的按钮填充该列。您还可以添加单击事件处理程序并相应地执行按钮功能。

tb.Columns.Add("ButtonColumn", typeof(Button));

foreach(DataTable tb  in result.Tables)
{
    foreach(DataRow r in tb.Rows)
    {
        r["ButtonColumn"] = new Button
                {
                    Name = "rowButton",
                    Content = "Row Button Content",
                    Width = 100,
                    Height = 30
                };
        tb.Rows.AddRow(r);
    }
}
相关问题