简体   繁体   中英

c# send data to dataset in another form

i have here a part of my code, where i send data from textboxes and datagridview to a dataset (all this in Form1)

But how i can do, that i send all these data to a dataset form (project > add new item > dataset ??

Thank you

private void SendData(object sender, EventArgs e)
    {
        dataGridView1.AllowUserToAddRows = false;

        DataSet ds = new DataSet();
        DataTable dt = new DataTable();
        dt.TableName = "Order";
        dt.Columns.Add("OrderNr");
        dt.Columns.Add("Custommer");
        dt.Columns.Add("Material");
        dt.Columns.Add("MaterialCode");
        ds.Tables.Add(dt);

        DataTable dtx = new DataTable();
        dtx.TableName = "Data";
        dtx.Columns.Add("Lenght");
        dtx.Columns.Add("Width");
        dtx.Columns.Add("Qty");
        dtx.Columns.Add("Texture");
        ds.Tables.Add(dtx);

        DataRow row = ds.Tables["Order"].NewRow();
        row["OrderNr"] = tbOrderNr.Text;
        row["Custommer"] = tbCustommer.Text;
        row["Material"] = tbMaterial.Text;
        row["MaterialCode"] = tbForm2MatCode.Text;
        ds.Tables["Udaje"].Rows.Add(row);

        foreach (DataGridViewRow r in dataGridView1.Rows)
        {
            DataRow row1 = ds.Tables["Data"].NewRow();
            row1["Lenght"] = r.Cells[0].Value;
            row1["Width"] = r.Cells[1].Value;
            row1["Qty"] = r.Cells[2].Value;
            row1["Texture"] = r.Cells[3].Value;
            ds.Tables["Data"].Rows.Add(row1);
        }
        dataGridView1.AllowUserToAddRows = true;
    }

Imagine your forms are called Form1 and Form2 and you want to send something to Form2 . You will do this:

Approach 1

In Form2 create a property:

public DataSet DataSet {get; set; }

Inf Form1 , imagine you have an instance of Form2 called f2 :

f2.DataSet = // The dataset you want to send to Form2

Approach 2

In Form1 (not Form2), create a property like this:

private DataSet aDataSet;
public DataSet DataSet {get { return this.aDataSet; }}

Then you can access it in Form2 using the Form1 instance. Imagine the instance is named f1 :

var f1DataSet = f1.DataSet;

Mr. JohnG posted here very usefull answer too, but was deleted. Thank you.

As CodingYoshi pointed out, there are several ways to pass variables between forms. It really depends on what you are doing. You could simply pass the WHOLE parent form to the child form like Form2 f2 = new Form2(this) where “this” is the parent Form1 and Form2 has a constructor with a signature like Form2(Form1 parent) . Inside that constructor you could assign “this” parent to a Form2 variable like: Form1 PARENT; then assign it in the constructor with PARENT = parent ;.

This would give you access to all the public variables in Form1 . Obviously this is overkill and unnecessary if we simply want to pass a single DataSet . So applying this idea, it seems fairly straight forward to pass the DataSet in Form1 to Form2 Example: Form2 f2 = new Form2(DataSet); . Obviously you will need to set up an appropriate constructor with a DataSet signature. This will pass the DataSet to Form2 as per the requirement.

The code below does just this with a few changes I felt would make this easier. I am guessing here, but following your code, it appears you have two (2) DataTables : Order and Data . The Order table appears to only hold ONE (1) record (row)… ie Order number; Customer; Material and Material Code are single entities for this Order . The Order may have one or more Data items ie Length, Width..Etc. This structure screams “make me a class”. The code below uses two (2) classes: Order and OrderItem .

OrderItem would be a single Data item in your code: “Length”, “Width” Etc.

Order would contain OrderNr , Customer etc. And a List of OrderItem objects to hold multiple OrderItems. Below a list is used but you could make it any data structure you may need.

This new Order class would be a convenient item to pass to that second form. Hope this make sense.

Barebone OrderItem class

public class OrderItem {
  public double Lenght { get; set; }
  public double Width { get; set; }
  public double Qty { get; set; }
  public string Texture { get; set; }

  public OrderItem(double inLength, double inWidth, double inQty, string inTexture) {
    Lenght = inLength;
    Width = inWidth;
    Qty = inQty;
    Texture = inTexture;
  }
}

Barebone Order Class – Addition method to get a DataTable of OrderItems and a string method.

public class Order {
  public int OrderNr { get; set; }
  public string Custommer { get; set; }
  public string Material { get; set; }
  public string MaterialCode { get; set; }
  public List<OrderItem> OrderInfo { get; set; }

  public Order(int oNum, string oCust, string oMaterial, string oMatCode) {
    OrderNr = oNum;
    Custommer = oCust;
    Material = oMaterial;
    MaterialCode = oMatCode;
    OrderInfo = new List<OrderItem>();
  }

  public DataTable GetItemsDT() {
    DataTable OrderDT = new DataTable();
    OrderDT.Columns.Add("Length", typeof(double));
    OrderDT.Columns.Add("Width", typeof(double));
    OrderDT.Columns.Add("Qty", typeof(double));
    OrderDT.Columns.Add("Texture", typeof(string));
    foreach (OrderItem item in OrderInfo) {
      OrderDT.Rows.Add(item.Lenght, item.Width, item.Qty, item.Texture);
    }
    return OrderDT;
  }

  public string GetOrderString() {
    StringBuilder sb = new StringBuilder();
    sb.AppendLine("Order: " + OrderNr + " Customer: " + Custommer + " Material: " + Material + " MatCode: " + MaterialCode);
    sb.AppendLine("-- Order Items --");
    sb.AppendLine("Length, Width, Qty, Texture");
    foreach (OrderItem item in OrderInfo) {
      sb.AppendLine(item.Lenght + ", " + item.Width + ", " + item.Qty + ", " + item.Texture);
    }
    sb.AppendLine("");
    return sb.ToString();
  }
}

Form1 methods GetOrderFromTextBoxes and GetOrderItemsFromDGV

private Order GetOrderFromTextBoxes() {
  int orderNum = 0;
  int.TryParse(tbOrderNr.Text, out orderNum);
  string cust = tbCustommer.Text;
  string mat = tbMaterial.Text;
  string code = tbForm2MatCode.Text;
  return new Order(orderNum, cust, mat, code);
}

private void GetOrderItemsFromDGV(Order inOrder) {
  double doubleValue = 0;
  double length = 0;
  double width = 0;
  double qty = 0;
  string texture = "";
  foreach (DataGridViewRow r in dataGridView1.Rows) {
    if (!r.IsNewRow) {
      if (r.Cells[0] != null)
        double.TryParse(r.Cells[0].Value.ToString(), out doubleValue);
      length = doubleValue;
      if (r.Cells[1] != null)
        double.TryParse(r.Cells[1].Value.ToString(), out doubleValue);
      width = doubleValue;
      if (r.Cells[2] != null)
        double.TryParse(r.Cells[2].Value.ToString(), out doubleValue);
      qty = doubleValue;
      if (r.Cells[3] != null)
        texture = r.Cells[3].Value.ToString();
      inOrder.OrderInfo.Add(new OrderItem(length, width, qty, texture));
    }
  }
}

Using a button click on Form1 to use the above methods and pass the Order object to Form2

private void btSendOrder_Click(object sender, EventArgs e) {
  Order thisOrder = GetOrderFromTextBoxes();
  GetOrderItemsFromDGV(thisOrder);
  Form2 f2 = new Form2(thisOrder);
  f2.ShowDialog();
}

Form2 using the incoming Order object to fill some text boxes and set the DataSource of a DataGridView by getting the Order's items DataTable with thisOrder.GetItemsDT();

private Order thisOrder;
public Form2(Order inOrder) {
  InitializeComponent();
  thisOrder = inOrder;
}

private void Form2_Load(object sender, EventArgs e) {
  tbCustommer.Text = thisOrder.Custommer;
  tbOrderNr.Text = thisOrder.OrderNr.ToString();
  tbMaterial.Text = thisOrder.Material;
  tbForm2MatCode.Text = thisOrder.MaterialCode;
  dataGridView1.DataSource = thisOrder.GetItemsDT();
}

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM