Displaying information in same form c# - c#

The following code is used by me to pass the data from 3 text boxes(in form 2) to a data grid view (in form 1). The data is passed successfully but the only problem I face is that data is passed to a new form of type f1!
private void button1_Click(object sender, EventArgs e)
{
Form1 f1 = new Form1();
DataTable dt1 = new DataTable();
f1.dataGridView2.DataSource = dt1;
dt1.Columns.Add("MessageID", typeof(string));
dt1.Columns.Add("Name", typeof(string));
dt1.Columns.Add("Number", typeof(string));
DataRow dr = dt1.NewRow();
dr["MessageID"] = IDtext.Text; ;
dr["Name"] = nameText.Text;
dr["Number"] = numberText.Text;
dt1.Rows.Add(dr);
f1.Show();
}
So each time I pass information a new window is created and the previous data i sent does not appear in the data grid view!How can i correct this?

Your refactored code:
class Form2
{
delegate void PassDataDelegate(DataTable dt1);
public event PassDataDelegate PassData;
private void button1_Click(object sender, EventArgs e)
{
DataTable dt1 = new DataTable();
dt1.Columns.Add("MessageID", typeof(string));
dt1.Columns.Add("Name", typeof(string));
dt1.Columns.Add("Number", typeof(string));
DataRow dr = dt1.NewRow();
dr["MessageID"] = IDtext.Text; ;
dr["Name"] = nameText.Text;
dr["Number"] = numberText.Text;
dt1.Rows.Add(dr);
// This is where you call your event
PassData(dt1);
}
}
class Form1
{
// Your existing Form1 class definition
private static void YourMethodWhereYouLaunchForm2()
{
Form2 f2 = new Form2();
// Add this handler and you will get it invoked whenever you ask from Form2
f2.PassData += Handle_DataPassed;
}
private void Handle_DataPassed(DataTable dt1)
{
// This is where you post the data now to the grid.
dataGridView2.DataSource = dt1;
}
}

Related

Data is inserted in this table but the datagridview shows as null values

table.Rows.Add(tb_customer.Text, tb_Item_Name.Text, tb_BatchNo.Text, tb_packing.Text,tb_quantity.Text, tb_discount.Text, tb_Price.Text, tb_total_Price.Text, tb_date.Text);
dgv_invoiceRecords.DataSource = table;
According to your title, you would like to insert the textbox value to datatable and use
datagridview to show it.
If you want to do it, please refer to the following code example.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
DataTable table = new DataTable();
private void button1_Click(object sender, EventArgs e)
{
table.Rows.Add(txtName.Text, txtAge.Text, txtID.Text, txtScore.Text);
dataGridView1.DataSource = table;
}
private void Form1_Load(object sender, EventArgs e)
{
table.Columns.Add("Name", typeof(string));
table.Columns.Add("Age", typeof(string));
table.Columns.Add("Id", typeof(string));
table.Columns.Add("Score", typeof(string));
}
}
Result:
If the above code can not solve your problem, you can provide the specific exception and a completed code.

How to save Datasource in a List<string> (no sqlconnection etc)

I have asked a similar question here but in my case I need to store multiple sets of datasets for an unknown number of times, not just once. So that is why I am asking here again.
I have multiple datatables with 2 columns in each datatable. One of them is a bool column (checkbox values). The checkbox values are empty when the form loads so up to user to check or uncheck them. Upon updating the checkbox, user press button1 to save only checkbox values in a dataset and this dataset will be saved in a List.
These datatables will then empty out and the same steps repeat for an unknown number of times (form loads empty datatables, user update checkboxes, user press button1). I have used the method below. No errors but when I want to display the List value in datagridview1 in Form2, it was empty. Below is my code. Hope to get help, thanks!
Class 1.cs (where I initiated my List)
public static List<string> list = new List<string>();
Form1.cs
//Create dataset
private DataSet Getdataset()
{
DataSet ds = new DataSet();
DataTable dt1 = new DataTable();
dt1.Columns.Add("Items", typeof(string));
dt1.Columns.Add("Status", typeof(bool));
dt1.Rows.Add("hello");
dt1.Rows.Add("hello");
ds.Tables.Add(dt1);
dgv1.DataSource = dt1;
dgv1.AllowUserToAddRows = false;
DataTable dt2 = new DataTable();
dt2.Columns.Add("Items", typeof(string));
dt2.Columns.Add("Status", typeof(bool));
dt2.Rows.Add("bye");
dt2.Rows.Add("bye");
ds.Tables.Add(dt2);
dgv2.DataSource = dt2;
dgv2.AllowUserToAddRows = false;
return ds;
}
//Save dataset in a List
private void button1_Click(object sender, EventArgs e)
{
DataSet dd = Getdataset();
foreach (DataTable table in dd.Tables)
{
foreach (DataRow row in table.Rows)
{
Class1.list.Add(Convert.ToString(row["Status"]));
}
}
Form2 f = new Form2();
f.ShowDialog();
}
Form2.cs
//Display dataset in datagridview
private void compile_VisibleChanged(object sender, EventArgs e)
{
DataGridViewRow row = (DataGridViewRow)dataGridView1.Rows[0].Clone();
foreach (string item in Class1.list)
{
row.Cells[0].Value = item;
}
dataGridView1.Rows.Add(row);
}
EDIT
As per er-sho comment, I added a messagebox in the code per below and it showed Systems.Collection.Generic.List1[System.String] 7 times (I have 6 checkboxes). There was still no display in the datagridview1 and I initialized the no. of columns, headers for datagridview1 to avoid compilation error. However, see below comment under Gokham’s solution (seems like the data in List is not per expected)
foreach (string item in Class1.list)
{
MessageBox.Show(Class1.list.ToString());
row.Cells[0].Value = item;
}
Everytime, when you click button1, you rewrite your data in dgv1 with default values:
DataTable dt1 = new DataTable();
dt1.Rows.Add("hello");
dt1.Rows.Add("hello");
dgv1.DataSource = dt1;
dt1 will be contains rows with only Item values, but not with Status. You should add code with getting user inputs
UPDATE
To achieve the goal you should make follow steps:
Init dgv1 - add columns and fill rows with default values
private void Form1_Load(object sender, EventArgs e)
{
UpdateDgv1();
}
private void UpdateDgv1()
{
DataSet ds = new DataSet();
DataTable dt1 = new DataTable();
dt1.Columns.Add("Items", typeof(string));
dt1.Columns.Add("Status", typeof(bool));
dt1.Rows.Add("hello");
dt1.Rows.Add("hello");
ds.Tables.Add(dt1);
dgv1.DataSource = dt1;
dgv1.AllowUserToAddRows = false;
}
Get values from dgv1 on button click
private void button1_Click(object sender, EventArgs e)
{
for (var i = 0; i < dgv1.Rows.Count; i++)
{
list.Add(Convert.ToString(dgv1.Rows[i].Cells[1].Value));
}
//restore dgv1
UpdateDgv1();
Form2 f = new Form2();
f.ShowDialog();
//clear list with old values or comment it
//if you want to save history of user inputs
list.Clear();
}
Show list content on datagridview1 of Form2
private void Form2_VisibleChanged(object sender, EventArgs e)
{
dataGridView1.ColumnCount = 1;
foreach (string item in Form1.list)
{
var newRow = dataGridView1.Rows.Add();
//if checkbox from Form1 not checked
if (item == string.Empty)
dataGridView1.Rows[newRow].Cells[0].Value = false.ToString();
else
dataGridView1.Rows[newRow].Cells[0].Value = item;
}
}
Your Getdataset() method doesn't fill Status column of the table. Try
private DataSet Getdataset()
{
DataSet ds = new DataSet();
DataTable dt1 = new DataTable();
dt1.Columns.Add("Items", typeof(string));
dt1.Columns.Add("Status", typeof(bool));
DataRow dr = dt1.NewRow();
dr["Items"] = "Hello";
dr["Status"] = checkBox1.Checked;
dt1.Rows.Add(dr);
ds.Tables.Add(dt1);
dgv1.DataSource = dt1;
dgv1.AllowUserToAddRows = false;
...
return ds;
}
I recommend you using BindingList instead of List and assigning the list as the DataSource for dataGridView1.
public static BindingList<string> list = new BindingList<string>();
Form 2
private void compile_VisibleChanged(object sender, EventArgs e)
{
dataGridView1.DataSource = Class1.list;
}
I think using FormLoad event handler instead of VisibleChanged should be better in your case.
Have a look : Binding List<T> to DataGridView in WinForm

Pass gridview data(one page) to another page's gridview control using winforms

I have two pages in my windows application. Both pages have a gridview control. I can pass textbox values into 2nd form but my problem is, I dont know how to pass whole gridview data into 2nd form.
Form1:
public partial class Billing : Form
{
DataTable dt;
public Billing()
{
InitializeComponent();
SetDataTable();
txtBillNo.Text = Convert.ToString(billno);
}
public void btnEnter_Click(object sender, EventArgs e)
{
int row = dgvBilling.RowCount;
DataRow dr;
if (dgvBilling.RowCount != 0)
{
dr = dt.NewRow();
dr["IName"] = txtIName.Text.Trim();
dr["Icode"] = txtIcode.Text.Trim();
dr["Quantity"] = txtQuantity.Text.Trim();
dr["UnitPrice"] = txtUnitPrice.Text.Trim();
int amount = Convert.ToInt32(txtQuantity.Text.Trim()) * Convert.ToInt32(txtUnitPrice.Text.Trim());
dr["amount"] = Convert.ToString(amount);
dt.Rows.Add(dr);
int total = 0;
for (int i = 0; i < dgvBilling.Rows.Count; ++i)
{
total += Convert.ToInt32(dgvBilling.Rows[i].Cells[5].Value);
txtTotal.Text = "Rs/" + "" + Convert.ToString(total);
}
}
else
{
//dt = SetDataTable();
dr = dt.NewRow();
dr["IName"] = txtIName.Text.Trim();
dr["Icode"] = txtIcode.Text.Trim();
dr["Quantity"] = txtQuantity.Text.Trim();
dr["UnitPrice"] = txtUnitPrice.Text.Trim();
int amount = Convert.ToInt32(txtQuantity.Text.Trim()) * Convert.ToInt32(txtUnitPrice.Text.Trim());
dr["Amount"] = Convert.ToString(amount);
dt.Rows.Add(dr);
int total = 0;
for (int i = 0; i < dgvBilling.Rows.Count; ++i)
{
total += Convert.ToInt32(dgvBilling.Rows[i].Cells[5].Value);
txtTotal.Text = "Rs/" + "" + Convert.ToString(total);
}
RetrieveData();
}
}
public DataTable SetDataTable()
{
dt = new DataTable();
DataColumn dc = new DataColumn("IName", typeof(string));
dt.Columns.Add(dc);
dc = new DataColumn("Icode", typeof(string));
dt.Columns.Add(dc);
dc = new DataColumn("Quantity", typeof(string));
dt.Columns.Add(dc);
dc = new DataColumn("UnitPrice", typeof(string));
dt.Columns.Add(dc);
dc = new DataColumn("Amount", typeof(string));
dt.Columns.Add(dc);
DataGridViewCheckBoxColumn checkBoxColumn = new DataGridViewCheckBoxColumn();
checkBoxColumn.HeaderText = "";
checkBoxColumn.Width = 50;
checkBoxColumn.Name = "checkBoxColumn";
dgvBilling.Columns.Insert(0, checkBoxColumn);
dgvBilling.DataSource = dt;
return dt;
}
private void btnPrintBill_Click(object sender, EventArgs e)
{
PrintBill pb = new PrintBill();
pb.Controls["lblNetAmount"].Text = txtTotal.Text;
pb.Show();
}
}
}
form2:
public partial class PrintBill : Form
{
public PrintBill()
{
InitializeComponent();
Billing bg = new Billing();
dataGridView1.DataSource = bg.RetrieveData();
}
}
When I click PrintBill button I want to show 2nd form with the gridview values of first form...
As a good option you can use a DataTable as data source of your DataGridView and when you need to pass data to other form, use Copy method of that DataTable. Here is an example:
Your GridForm:
public partial class GridForm : Form
{
public GridForm()
{
InitializeComponent();
}
//we will this as data source
private DataTable table;
private void GridForm_Load(object sender, EventArgs e)
{
//Create the data table here and bind it to grid
table = new DataTable();
table.Columns.Add(new DataColumn("IName", typeof(string)));
table.Columns.Add(new DataColumn("Icode", typeof(string)));
table.Columns.Add(new DataColumn("Quantity", typeof(string)));
table.Columns.Add(new DataColumn("UnitPrice", typeof(string)));
table.Columns.Add(new DataColumn("Amount", typeof(string)));
this.dataGridView1.DataSource = table;
}
private void passDataButton_Click(object sender, EventArgs e)
{
//When you need to pass data, use Copy method of data table
var clone = table.Copy();
//Pass data to other form
var f = new OtherForm(clone);
f.ShowDialog();
}
}
Your OtherForm:
public partial class OtherForm : Form
{
DataTable table;
public OtherForm(DataTable value)
{
InitializeComponent();
//get passed data and put it in some member variable for next usages
table = value;
//Bind data to other grid or do other things here or in Form Load event
this.dataGridView1.DataSource = table;
}
}
Actually, you can make that datarow/DataTable a global variable to make it accessible to other class. And also, you can create a public method that returns a datatable like :
public DataTable RetrieveData()
{
DataTable dt;
//retrive necessary data here
return dt;
}

How to add a row into datatable on button click event?

In Windows Forms, I want to add row by row into DataGridView on button click event by taking the values from other controls. I am using DataTable in this case and it is bound to DataGridView.
I am using the following code and it is working fine when the data is inserted for the first time. But my problem is when I click the button for the second time, the first row is overwritten with the second row of data.
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;
}
In ASP .NET applications, I use the session state to check whether DataTable is null by using the following code:
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);
}
}
What can I do and how can I make the changes to get the right solution in Windows Forms? Any help will be much appreciated!
In GetAdjustmentTable, you are recreating adjustmentTable every time. So a new row will just overwrite the existing one.
You need to modify the code such that adjustmentTable is only created just once and subsequent calls add rows to it. One way to do that would be to make it a private field and to check if it's null, and create it if it is:
private DataTable _adjustmentTable;
private DataTable GetAdjustmentTable()
{
if (adjustmentTable == null)
{
adjustmentTable = new DataTable();
}
....

How to extract data from a TextBox to a GridView (multiple rows to be displayed in the GridView)?

I have used the following code to display data from a TextBox to a GridView without saving the data to the database(Two TextBoxes and a Button). When I enter the Name and City in the TextBoxes, and click on the Button, those values will be displayed in the Gridview. It is working as expected without any errors. But I want to tweak the code a bit so that the GridView should be able to add new data from the Textbox by retaining the old data as it is in the Gridview (multiple rows should be displayed in the Gridview instead of single rows).
It is a web-based ASP.NET application using C# coding (Visual Studio 2010).
Can you make the necessary changes to the code given below so as to implement the above functionality?
public partial class _Default : System.Web.UI.Page
{
DataSet ds = new DataSet();
DataTable dt = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnTextDisplay_Click(object sender, EventArgs e)
{
DataColumn dc1 = new DataColumn("Name");
DataColumn dc2 = new DataColumn("City");
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
DataRow dr = dt.NewRow();
dr[0] = txtName.Text;
dr[1] = txtCity.Text;
dt.Rows.Add(dr);
gvDisplay.DataSource = dt;
gvDisplay.DataBind();
}
}
You have to persists your data between postbacks, you have many options here: by Session, ViewState, Cache and some other.
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostback)
{
dt = Session["data_table"] as DataTable;
}
}
protected void btnTextDisplay_Click(object sender, EventArgs e)
{
if (dt == null)
{
dt = new DataTable();
DataColumn dc1 = new DataColumn("Name");
DataColumn dc2 = new DataColumn("City");
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
}
DataRow dr = dt.NewRow();
dr[0] = txtName.Text;
dr[1] = txtCity.Text;
dt.Rows.Add(dr);
gvDisplay.DataSource = dt;
gvDisplay.DataBind();
Session["data_table"] = dt;
}

Categories