How to display a datatable in the view? - c#

I have a visual studio windows from called managerForm. Clicking a button on the manager form will bring up a filter form. Also, I have a separate dataGridView Form.
By clicking "SELECT" button on the filter form, I will get a SQL dataTable. I want to display the table on the dataGridView Form.
I don't know how to connect them together. Any help?
dg.myDatagridView.DataSource = filter.returnedDataList;
In this code, I cannot get the returnedDataList since filter closed.
private void btnDisplayFilter_Click(object sender, EventArgs e)
{
filterForm filter = new filterForm();
filter.ShowDialog(this);
displayGridViewControl dg = new displayGridViewControl();
dg.myDatagridView.DataSource = filter.returnedDataList;
displayGridView.ShowDialog();
}

You could try with using statement
private void btnDisplayFilter_Click(object sender, EventArgs e)
{
using(filterForm filter = new filterForm())
{
if(filter.ShowDialog(this) == DialogResult.OK)
{
displayGridViewControl dg = new displayGridViewControl();
dg.myDatagridView.DataSource = filter.returnedDataList;
displayGridView.ShowDialog();
}
}
}
This will keep the instance of filterForm still available and you could get the public properties of this instance until your code reaches the closing brace of the using statement

Related

transfer data from one form dataGridView to another form TextBox

I have dataGridView in Form1 and I need to transfer the data that in selected row to another form text box.
When someone clicks on a row and then clicks on update button...
FORM1
the following screen will occur with the referenced data.
AddUpdateForm
The following code is for the UPDATE button
private void updateBtn_Click(object sender, EventArgs e)
{
AddUpdateForm addUpdate = new AddUpdateForm();
addUpdate.ShowDialog();
}
This code is for the click event on dataGridViewPRoducts
private void dataGridViewProducts_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
AddUpdateForm addUpdate = new AddUpdateForm();
Products pro = new Products();
if (e.RowIndex >= 0)
{
DataGridViewRow row = dataGridViewProducts.Rows[e.RowIndex];
pro.Name = row.Cells["name"].Value.ToString();
pro.Brand = row.Cells["brand"].Value.ToString();
pro.Item_Price = Convert.ToDouble(row.Cells["item_price"].Value.ToString());
pro.StokType =row.Cells["stock_type"].Value.ToString();
pro.Stock_Amount = Convert.ToDouble(row.Cells["stock_amount"].Value.ToString());
textBox1.Text = row.Cells["name"].Value.ToString();
}
Actually I know i need to return the pro object to the UPDATE button. However, I don't know how to do it.
From your comment about the two problems…
”First one, AddUpdateForm does not except a pro object.”
This is true, and my comment implied that you should create a constructor that "does" accept a pro object. Then the form can access the passed in Products object. Something like…
private Products currentProduct;
public AddUpdateForm(Products passedInProduct) {
currentProduct = passedInProduct;
InitializeComponent();
}
private void AddUpdateForm_Load(object sender, EventArgs e) {
// here you can use the passed in Products object called currentProduct
}
Your second Comment…
”The second one is when I move the code, I need a DataGridViewCellEventArgs object which I have a EventArgs in
updateBtn_Click.”
This is not true. You do not need the DataGridViewCellEventArgs object. In its current state, by setting the pro object in the grids CellContentClick event means that ONLY if the user “clicks” on a cell then the pro object gets set… what if the user pressed the down arrow instead of clicking? If the user never clicks a cell, pro will be null.
You want to grab the currently “selected” row no matter how the user “selected" it. So it makes no sense to grab this row until the user presses the “update” button. Then we simply select the grids CurrentRow… i.e. … the girds currently selected row.
Therefore, the update button click event may look something like…
private void updateBtn_Click(object sender, EventArgs e) {
DataGridViewRow row = dataGridViewReport.CurrentRow;
Products pro = new Products {
Name = row.Cells["name"].Value.ToString(),
Brand = row.Cells["brand"].Value.ToString(),
Item_Price = Convert.ToDouble(row.Cells["item_price"].Value.ToString()),
StokType = row.Cells["StokType"].Value.ToString(),
Stock_Amount = Convert.ToDouble(row.Cells["Stock_Amount"].Value.ToString())
};
AddUpdateForm addUpdate = new AddUpdateForm(pro);
addUpdate.ShowDialog();
}
Or if the grids data source is a List<Products> then instead of grabbing the values from the individual cells, you could just get the DataBoundItem from the row and cast it to a Products object like...
Products pro = (Products)dataGridViewReport.CurrentRow.DataBoundItem;
I hope this makes sense.

Is there a way to filter data showing in crystal reports from combobox value in another form

I want to show a specified employee in crystal report when choosing his ID in another form, so i have to pass the combobox value from form2 to CrystalReport to filter with it...
I tried so many things, so many lines of code, but nothing seems to be helpful, and i'm in great need to finish this project
in Form2.cs
private void btn_Imprimer_Click(object sender, EventArgs e)
{
RptSalarié r = new RptSalarié();
r.DataSourceConnections[0].IntegratedSecurity = true;
FrmImpression f = new FrmImpression(r);
f.ShowDialog();
}
in CrystalReport.cs
private void FrmImpression_Load(object sender, EventArgs e)
{
crystalReportViewer1.ReportSource = Report;
crystalReportViewer1.Refresh();
}
Try something like:
report.RecordSelectionFormula = "{Table.Field1} = '123'"
It's basically like a where statement in a normal SQL query and just filters your results.

How to Transfer data from datagridview to the active form?

I am working on GYM Desktop application.
Here with Product Entry form I insert the data in database table. And when I click the 'get data' button all the records appear in different form (Product_List) in datagridview.
Now on double clicking on any particular row(in Product_List form) I want to get the data of that particular row in my currently active Product_Entry Form, so i can update or delete the data.
so how can i get the data of that row on currently active Product Entry form ?
here is the code on mouse double_click on row in datagridview:
private void dataGridView1_MouseDoubleClick(object sender, MouseEventArgs
e)
{
Product_Entry pe = new Product_Entry();
pe.textBox1.Text =
dataGridView1.CurrentRow.Cells[0].Value.ToString();
pe.textBox2.Text =
dataGridView1.CurrentRow.Cells[1].Value.ToString();
pe.comboBox1.Text =
dataGridView1.CurrentRow.Cells[2].Value.ToString();
pe.comboBox2.Text =
dataGridView1.CurrentRow.Cells[3].Value.ToString();
pe.textBox3.Text =
dataGridView1.CurrentRow.Cells[4].Value.ToString();
pe.textBox4.Text =
dataGridView1.CurrentRow.Cells[5].Value.ToString();
pe.textBox5.Text =
dataGridView1.CurrentRow.Cells[6].Value.ToString();
pe.textBox6.Text =
dataGridView1.CurrentRow.Cells[7].Value.ToString();
pe.ShowDialog();
}
Use of global variable may solve this. declare a global variable like
public static string ProductId= ""; in your datagridview form. Then on dataGridView1_MouseDoubleClick event set your productId like
private void dataGridView1_MouseDoubleClick(object sender, MouseEventArgs e)
{
ProductId = dataGridView1.CurrentRow.Cells[0].Value.ToString();
Form2 frm2 = new Form2(); //your second form
frm2.Show();
}
Then in second form (where you want to show the value) receive the ProductId in Form2_Load event like
private void Form2_Load(object sender, EventArgs e)
{
label1.Text = Form1.ProductId; //label is your control to show data
}
Like this you can receive all data in second form or you can write a database query with productid to fetch data from database. For more see this

DevExpress Gridview can't show data

I have this code :
private void frmWeld_Load(object sender, EventArgs e)
{
List<Weld> lst = _weldRepository.Get().ToList();
gridControl.DataSource = new BindingList<Weld>(lst) { AllowNew = true };
}
I want to load my data into devExpressGridView
As you can see my data is loaded but the gridview can't show the data and my break point doesn't pass from gridControl.DataSource = new BindingList<Weld>(lst) { AllowNew = true} and my program remains with this state.
Why?
I just add new column to my gridview after this the problems occurreded.
I use entity framework ,when i change the database my application creates a new database using code first and after that my data is lost but the problem that i said is solved.
Since your code is executing in Load event, add ForceInitialize() to your code.
private void frmWeld_Load(object sender, EventArgs e)
{
// your previous code
gridControl.ForceInitialize(); <- add this line
}

DataGridView bound to EF collection not updating

There are a ton of postings on related topics but I can't figure out what in the world is going on. I have a winform app containing a datagrid that displays the contents of a SQL table (a list of accounts). I'm using the the entity framework model for data management.
Intended functionality is as follows. When the main form launches, it creates an instance of the data model called "ODS." The data is then linked to the datagrid via the following call in the form's load() method:
grid_accounts.DataSource = ODS.Accounts;
When an "add account" button is clicked on the main form, it opens a dialog for the user to fill in the new account info. The data source is passed to the dialog. If the user clicks OK, the new account entity is created, added to ODS.Accounts, saved, and the dialog closed. The datagrid on the main form should then show the new account.
All of this happens as intended EXCEPT the new account doesn't appear in the datagrid. I've checked, and it IS saved to the database once SaveChanges() is called. I've also outputted the contents of ODS.Accounts to the console and the new entity is indeed in the collection. I've tried calling Refresh() on the datagrid; I've also tried the well-known trick of setting the grid's dataSource to null prior to resetting it to the Account collection to "fool" the grid into updating. No luck. Am I missing something obvious? Relevant code below. Thanks.
The MainForm:
public partial class MainForm : Form
{
VHN.DataAccess.ODSEntities ODS = VHN.DataAccess.Util.getODS();
private void Load(object sender, EventArgs e)
{
populate();
}
private void button_addAccount_Click(object sender, EventArgs e)
{
var d = new NewAccountForm(ODS);
if (d.ShowDialog() == System.Windows.Forms.DialogResult.OK)
populate();
}
void populate()
{
grid_accounts.DataSource = null;
grid_accounts.DataSource = ODS.Accounts;
grid_accounts.Columns[0].Visible = false;
grid_accounts.Refresh();
}
}
The new account dialog:
public partial class NewAccountForm : Form
{
VHN.DataAccess.ODSEntities ODS;
public NewAccountForm(ref VHN.DataAccess.ODSEntities ODS)
{
this.ODS = ODS;
InitializeComponent();
}
private void ok_Click(object sender, EventArgs e)
{
int count = ODS.Accounts.Where(x => x.name == tb_name.Text).Count();
if (count > 0)
{
MessageBox.Show(String.Format("Account name \"'{0}\" is already taken.", tb_name.Text));
}
else
{
VHN.DataAccess.Account account = new VHN.DataAccess.Account();
account.id = Guid.NewGuid().ToString();
account.name = tb_name.Text;
ODS.Accounts.AddObject(account);
ODS.SaveChanges();
this.DialogResult = System.Windows.Forms.DialogResult.OK;
Close();
}
}
}
Try one of these: (Assuming Accounts is a List type
grid_accounts.EndEdit();
grid_accounts.Refresh();
OR
grid_accounts.DataSource = typeof(List);
grid_accounts.DataSource = ODS.Accounts;
Update:
grid_accounts.DataSource = typeof(List<>); // OR use grid_accounts.DataSource = null;
grid_accounts.DataSource = ODS.Accounts.ToList();
if you are still on this.
grid_accounts.DataSource = new BindingSource(ODS.Accounts);
should work just fine.

Categories

Resources