2013-11-04 78 views
1

Windows窗體,我想通過從其他控件的值中逐行添加到按鈕單擊事件的DataGridView。在這種情況下,我使用DataTable並將其綁定到DataGridView。如何在按鈕點擊事件的數據表中添加一行?

我正在使用下面的代碼,並且它在第一次插入數據時工作正常。但是我的問題是當我第二次點擊按鈕時,第一行被第二行數據覆蓋。

private void btnAddToGrid_Click(object sender, EventArgs e) 
{ 
     LoadDataGridView(); 
} 

private void LoadDataGridView() 
{ 
     dgvAdjustment.DataSource = GetAdjustmentTable(); 
} 

private DataTable GetAdjustmentTable() 
{ 
     DataTable adjustmentTable = new DataTable(); 

     DataColumn dataColumn; 
     dataColumn = new DataColumn("SourceOutletID", typeof(int)); 
     adjustmentTable.Columns.Add(dataColumn); 
     dataColumn = new DataColumn("DestinationOutletID", typeof(int)); 
     adjustmentTable.Columns.Add(dataColumn); 
     dataColumn = new DataColumn("TransactionDate", typeof(DateTime)); 
     adjustmentTable.Columns.Add(dataColumn); 
     dataColumn = new DataColumn("MaterialName", typeof(string)); 
     adjustmentTable.Columns.Add(dataColumn); 
     dataColumn = new DataColumn("AdjustmentType", typeof(int)); 
     adjustmentTable.Columns.Add(dataColumn); 
     dataColumn = new DataColumn("CurrentBalance", typeof(decimal)); 
     adjustmentTable.Columns.Add(dataColumn); 
     dataColumn = new DataColumn("AdjustmentQty", typeof(decimal)); 
     adjustmentTable.Columns.Add(dataColumn); 
     dataColumn = new DataColumn("NewBalance", typeof(decimal)); 
     adjustmentTable.Columns.Add(dataColumn); 

     DataRow dataRow = adjustmentTable.NewRow(); 
     dataRow[0] = cmbSourceOutlet.SelectedValue; 
     dataRow[1] = cmbDestinationOutlet.SelectedValue; 
     dataRow[2] = TransDateTimePicker.Value; 
     dataRow[3] = cmbMaterialName.SelectedValue; 
     dataRow[4] = cmbAdjustmentType.SelectedValue; 
     dataRow[5] = Convert.ToDecimal(lblCurBalVal.Text); 
     dataRow[6] = Convert.ToDecimal(lblAdjVal.Text); 
     dataRow[7] = Convert.ToDecimal(lblNewQtyVal.Text); 

     int insertPosition = adjustmentTable.Rows.Count; 
     adjustmentTable.Rows.InsertAt(dataRow, insertPosition); 

     return adjustmentTable; 
} 

ASP .NET應用程序,我使用會話狀態,檢查數據表是否爲使用下面的代碼空:

protected void Button1_Click(object sender, EventArgs e) 
{ 
    try 
    { 
    //Check if previous session is exist 
    if (Session["MyTable"] == null) 
    { 
     dtMyTable = new DataTable("MyTable"); 
     dtMyTable.Columns.Add("Id", typeof(int)); 
     dtMyTable.Columns.Add("LName", typeof(string)); 

    } 
    else 
    { 
     //If yes then get it from current session 
     dtMyTable = (DataTable)Session["MyTable"]; 
    } 

    //Add new row every time 
    DataRow dt_row; 
    dt_row = dtMyTable.NewRow(); 
    dt_row["Id"] = TextBox1.Text; 
    dt_row["LName"] = TextBox2.Text; 
    dtMyTable.Rows.Add(dt_row); 

    //Update session table 
    Session["MyTable"] = dtMyTable; 
    } 
    catch (Exception ex) 
    { 

    Response.Write(ex.Message); 
    } 
} 

我能做些什麼,我怎麼能做出改變在Windows Forms中獲得正確的解決方案?任何幫助都感激不盡!

回答

1

GetAdjustmentTable中,您每次都重新創建adjustmentTable。所以一個新的行將會覆蓋現有的行。

您需要修改代碼,以使adjustmentTable只創建一次,隨後的調用添加到該代碼中。要做到這一點將是使它成爲一個私人領域,並檢查它是否爲空,並創建它,如果它是:

private DataTable _adjustmentTable; 

private DataTable GetAdjustmentTable() 
{ 
    if (adjustmentTable == null) 
    { 
     adjustmentTable = new DataTable(); 
    } 
    .... 
+0

你是對的!我在方法外部聲明瞭DataTable,並檢查DataTable是否爲空。謝謝你的幫助。 –

相關問題