我一直在将数据插入同一行

时间:2016-11-17 05:07:17

标签: c#

请帮助弄清楚我做错了什么,我的datagridview是数据绑定所以我不能添加一个新行,所以我创建了一个数据表,以便能够这样做,有一个问题,我可以'摆脱,因为我尝试添加数据,我添加到同一行。而不是新的行,请帮助解决为什么会发生这种情况或更好的解决方法。

public void getAllData(string stockCode, string description, string unit, string quantity, string costPrice,
          string saleprice, string taxcode)
{
    DataTable table = new DataTable();

    DataRow dr = table.NewRow();
    table.Columns.Add(new DataColumn("stk_code"));
    table.Columns.Add(new DataColumn("stk_description"));
    table.Columns.Add(new DataColumn("quantity"));
    table.Columns.Add(new DataColumn("uom"));
    table.Columns.Add(new DataColumn("discount"));
    table.Columns.Add(new DataColumn("tax_code"));
    table.Columns.Add(new DataColumn("stk_sale_price_one"));

    dr[0] = stockCode;
    dr[1] = description;
    dr[2] = quantity;
    dr[3] = 0;
    dr[4] = unit;
    dr[5] = taxcode;
    dr[6] = saleprice;

    invoiceItemDataView.DataSource = table;

    table.Rows.Add(dr);
    table.AcceptChanges();
}

3 个答案:

答案 0 :(得分:1)

为什么每次设置行都会创建Table列?你不需要那个。在程序开始时执行一次。就像这样。

创建表格:

  private DataTable CreateDataTable()
    {

        DataTable table = new DataTable();

        table.Columns.Add(new DataColumn("stk_code"));
        table.Columns.Add(new DataColumn("stk_description"));
        table.Columns.Add(new DataColumn("quantity"));
        table.Columns.Add(new DataColumn("uom"));
        table.Columns.Add(new DataColumn("discount"));
        table.Columns.Add(new DataColumn("tax_code"));
        table.Columns.Add(new DataColumn("stk_sale_price_one"));

        return table;
    }

将其初始化为您将设置为DataGridView的DataTable:

   private DataTable dt;

    public MyClass()
    {
        InitializeComponent();
        dt = CreateDataTable();
    }

现在,将数据表发送到您的方法。

getAllData(dt, "1", "2", "2", "2",
                  "2", "s", "s");
            dataGridView1.DataSource = dt;

最后,将新行插入Datatable:

public void getAllData(DataTable table, string stockCode, string description, string unit, string quantity, string costPrice,
      string saleprice, string taxcode)
    {
        int index = table.Rows.Count > 0 ? table.Rows.Count : 0;
        DataRow dr = table.NewRow();
        dr[0] = stockCode;
        dr[1] = description;
        dr[2] = quantity;
        dr[3] = 0;
        dr[4] = unit;
        dr[5] = taxcode;
        dr[6] = saleprice;

        table.Rows.InsertAt(dr, index);
        table.AcceptChanges();
    }

希望这对你有所帮助。

答案 1 :(得分:0)

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    deities deity_data[] = new deities[]
            {
                    new deities(R.drawable.transparent, "Test 1", "http://google.com", 1, Color.parseColor("#ff3399")),
                    new deities(R.drawable.transparent, "Test 2", "http://facebook.com", 1, Color.parseColor("#9933ff")),
                    new deities(R.drawable.transparent, "Test 3", "http://youtube.com", 1, Color.parseColor("#0099ff")),
                    new deities(R.drawable.transparent, "Test 4", "http://pinterest.com", 1, Color.parseColor("#009900")),
                    new deities(R.drawable.transparent, "Test 5", "http://twitter.com", 1, Color.parseColor("#cc0000")),
                    new deities(R.drawable.transparent, "Test 6", "http://facepunch.com", 1, Color.parseColor("#0000cc")),
                    new deities(R.drawable.transparent, "Test 7", "http://linustechtips.com", 1, Color.parseColor("#333300"))

            };
    deityListAdapter adapter = new deityListAdapter(this,
            R.layout.deity_list_item, deity_data);

    ListView deityList = (ListView)findViewById(R.id.deityList);
    deityList.setAdapter(adapter);
    WebView webView = (WebView)findViewById(R.id.webView);
    webView.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress) {
            ProgressBar webViewProgressBar = (ProgressBar) findViewById(R.id.webViewProgress);
            webViewProgressBar.setVisibility(View.VISIBLE);
            webViewProgressBar.setProgress(progress);
                    if (progress == 100) {
                        webViewProgressBar.setVisibility(View.GONE);
                    }
                }
            });
    webView.setWebViewClient(new WebViewClient() {
        @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url)
                {
                    return false;
                }

                @Override
                public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request)
                {
                    return false;
                }
            });

            webView.getSettings().setJavaScriptEnabled(true);
            webView.setVisibility(View.VISIBLE);






}


@Override
public void onBackPressed(){
    WebView webView = (WebView)findViewById(R.id.webView);
    ProgressBar progressBar = (ProgressBar)findViewById(R.id.webViewProgress);
    if (webView.getVisibility() == View.VISIBLE) {
        webView.setVisibility(View.GONE);
        progressBar.setVisibility(View.GONE);
    }

}
}

答案 2 :(得分:-1)

static DataTable MyTable = null; //store the table data;

//may be you means "AddData"?
public void AddData(string stockCode, string description, string unit, string quantity, string costPrice,
          string saleprice, string taxcode)
{
    DataTable table = MyTable
    if(table == null)
    {
        DataTable table = new DataTable();    
        table.Columns.Add(new DataColumn("stk_code"));
        table.Columns.Add(new DataColumn("stk_description"));
        table.Columns.Add(new DataColumn("quantity"));
        table.Columns.Add(new DataColumn("uom"));
        table.Columns.Add(new DataColumn("discount"));
        table.Columns.Add(new DataColumn("tax_code"));
        table.Columns.Add(new DataColumn("stk_sale_price_one"));
        MyTable = table;
    }

    DataRow dr = table.NewRow();
    dr[0] = stockCode;
    dr[1] = description;
    dr[2] = quantity;
    dr[3] = 0;
    dr[4] = unit;
    dr[5] = taxcode;
    dr[6] = saleprice;
    table.Rows.Add(dr);
    table.AcceptChanges();

    invoiceItemDataView.DataSource = table;
}