如何使用c#在WPF中添加行到gridview

本文关键字:添加行 gridview WPF 何使用 | 更新日期: 2023-09-27 18:12:10

当网格视图已经包含数据时,如何在WPF中向网格视图添加行,

            SqlCommand cmd = new SqlCommand("SELECT  CName'Name',CLocation'Location',VehicleNo,GasName,Quantity,OrderDate,SupplyDays'Dispatching Day' FROM  View_DailyPlanning "
            + " where  (OrderDate='" + DateTime.Now.ToShortDateString() + "' and SupplyDays='" + day + "')", DataAccessBase.GetSqlConnection());
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable("View_DailyPlanning");
            da.Fill(dt);
            grid_display.AutoGenerateColumns = true;
            grid_display.ItemsSource = dt.DefaultView;
            grid_display.Columns[0].Width = 100;
            grid_display.Columns[1].Width = 150;
            grid_display.Columns[2].Width = 100;
            grid_display.Columns[3].Width = 100;
            grid_display.Columns[4].Width = 100;
            grid_display.Columns[5].Width = 100;
            grid_display.Columns[6].Width = 100;
            grid_display.Items.Add(new Item() { CName = "Someone4", CLocation = "Madivala",VehicleNo="TN2345",Quantity="26",Date="10/07/2014",SupplyDays="Friday"});

ction());
                SqlCommand cmd = new SqlCommand("SELECT  CName'Name',CLocation'Location',VehicleNo,GasName,Quantity,OrderDate,SupplyDays'Dispatching Day' FROM  View_DailyPlanning "
                + " where  (OrderDate='" + DateTime.Now.ToShortDateString() + "' and SupplyDays='" + day + "')", DataAccessBase.GetSqlConnection());
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable("View_DailyPlanning");
                da.Fill(dt);
                grid_display.AutoGenerateColumns = true;
                grid_display.ItemsSource = dt.DefaultView;
                grid_display.Columns[0].Width = 100;
                grid_display.Columns[1].Width = 150;
                grid_display.Columns[2].Width = 100;
                grid_display.Columns[3].Width = 100;
                grid_display.Columns[4].Width = 100;
                grid_display.Columns[5].Width = 100;
                grid_display.Columns[6].Width = 100;
                grid_display.Items.Add(new Item() { CName = "Someone4", CLocation = "xxx",VehicleNo="1234",Quantity="26",Date="10/07/2014",SupplyDays="Friday"});

如何添加额外的行到网格视图,有什么想法吗?

如何使用c#在WPF中添加行到gridview

那么从头开始,您可以使用以下代码添加新行

DataTable dt = new DataTable();
if (dt.Columns.Count == 0)
{
    dt.Columns.Add("ColumnA", typeof(string));
    dt.Columns.Add("ColumnB", typeof(string));
}
DataRow NewRow = dt.NewRow();
NewRow[0] = "Some Text";
NewRow[1] = "Some Other Text";
dt.Rows.Add(NewRow); 
GridView1.DataSource = dt;
GridViewl.DataBind();

使用

添加其他行
DataRow NewRow = dt.NewRow();

希望对大家有所帮助