如何使用C#在WPF中向gridview添加行

时间:2014-07-09 08:47:15

标签: c# wpf datagridview

当网格视图已包含数据时,如何在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"});

如何向网格视图中添加其他行,以及执行此操作的任何想法。

1 个答案:

答案 0 :(得分:1)

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

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();

我希望这会有所帮助