Enquiry about reportViewer taking parameters from a form - c#

I'm quite new to using ReportViewer. I have a problem that I do not know how to solve.
I have a Date TextBox and a Location TextBox. My problem is that, when the user keys in the date and location, how do I display the same data in the ReportViewer. Below are some screenshots for better understanding. Any help will be appreciated.
Please let me know if more details are required.

I am assuming you'll add a ShowButton button to your Form. However, you can use TextBox event (e.g. Focus Lost, Text Changed etc) instead of button click event.
private void ShowButton_Click(object sender, EventArgs e)
{
DateTime allocationDate = Convert.ToDateTime(allocDateTextBox.Text);
string locationName = locationTextBox.Text;
//You can create as many datasources matching the name of DataSet in your report
ReportDataSource rds = new ReportDataSource("DataSet1");
rds.Value = getData(String.Format("Select * from myTable where theDate={0} AND Location={1}", allocationDate, locationName));
reportViewer1.LocalReport.DataSources.Clear();
//add as many datasources created above
reportViewer1.LocalReport.DataSources.Add(rds);
reportViewer1.RefreshReport();
}
private DataTable getData(string query)
{
//Select new record from database based on the new query with the new criteria
//return the record as DataTable, DataSet or Collection of object
}

Related

How to show the dataGrid from a text search filter with c# in postgresql

I have a text button that should filter the name of a person from a data table. the code is:
private void txtSearch_TextChanged(object sender, EventArgs e)
{
DataView DT = new DataView(dataTable);
DT.RowFilter = string.Format("nome_docente LIKE '%{0}%'", txtSearch.Text);
dgvTabela.DataSource = DT;
}
When running the code after the table loads, when I search for a name all the table disappears, and nothing is shown.
Can someone give me a tip on this?
Best regards

Subreport In Reportviewer Table

I want to show a ticket list in report viewer and provide printing feature for each ticket and the whole list and also paging the whole list to 3 , 5 or 7 segments. but I have stuck in the first step :)
I am using subreport in reportviwer in a WindowsFormApplication.
This is my MainReport and SubReport:
This is my code after setting ReportEmbeddedResource :
reportViewer1.LocalReport.DataSources.Clear();
ReportDataSource data = new ReportDataSource("DataSet1", dt);
reportViewer1.LocalReport.DataSources.Add(data);
this.reportViewer1.LocalReport.SubreportProcessing += new SubreportProcessingEventHandler(SubreportProcessingEventHandler);
reportViewer1.RefreshReport();
The code in the SubreportProcessingEventHandler:
int i = 0;
void SubreportProcessingEventHandler(object sender , SubreportProcessingEventArgs e)
{
DataRow dr = dt.Rows[i];
e.DataSources.Add(new ReportDataSource("DataSet1", (object)dr.Table));
i++;
}
Because my report should show two tickets this EventHandler lauches two times.Thus, I stored each row of my table(dt) in a DataRow which is named dr. It seems this EventHandler just uses only the first DataRow content. And the result is repetitive tickets. Here's the output:
Note: I am completely sure about the value in the dr. It has been checked before.
I know I should some how tell the SubReport to use different DataSource for each ticket, but I don't know how to achieve this goal.
Any help would be greatly appreciated.Thank you in advance.
I finally solved this problem by passing parameter from main report to subreport
and I edited my EventHandler like this:
void SubreportProcessingEventHandler(object sender , SubreportProcessingEventArgs e)
{
e.DataSources.Add(new ReportDataSource("DataSet1", dt));
}

C# 2015 - Gridview selected rows to RDLC report - Per page one row

I am working on C# 2015 windows application.
The basic idea is to get the records from the database and display in to the gridview. And print the selected records of grid view using RDLC report.
See the screenshot below for the basic idea.
Now, my print button code is as below:
private void btnPrint_Click(object sender, EventArgs e)
{
List<customer> lstCustomer = new List<customer>();
foreach (DataGridViewRow row in dgCustomers.SelectedRows)
{
customer c = new customer();
c.id = Convert.ToInt32(row.Cells[dgCustomers.Columns["id"].Index].Value);
c.firstName = row.Cells[dgCustomers.Columns["first_name"].Index].Value.ToString();
c.firstName = row.Cells[dgCustomers.Columns["last_name"].Index].Value.ToString();
lstCustomer.Add(c);
}
frmReport r = new frmReport();
r.Show();
ReportViewer v = r.Controls.Find("reportViewer1", true).FirstOrDefault() as ReportViewer;
v.LocalReport.ReportEmbeddedResource = "bolt.rptBolt.rdlc";
ReportDataSource dataset = new ReportDataSource("ReportDataSet1", lstCustomer);
v.LocalReport.DataSources.Clear();
v.LocalReport.DataSources.Add(dataset);
v.LocalReport.Refresh();
v.RefreshReport();
this.Hide();
}
See, I have created a List lstCustomer of class customer and added objects of the selected rows' cell values into it.
I have created a new form frmReport to display the report and added a reportviewer into it.
I have also taken an Rdlc report named rptBolt.rdlc and added it to the report viewer.
I have set the datasource of report with my list.
But now, I am not sure how to bind id, first name and last name into the report's text boxes.
P.S. I want separate page for each record.
I am not sure how to do it. Can anyone knows?
You can do like this,
In the frmReport, create one property called as ReportId and use as below.
/* Below Code goes to frmReport.cs */
//after the frmReport()
public int ReportId;
//In your forms where u have rdlc
frmReport r = new frmReport();
r.ReportId = GetTheSelectedRecordFromGridView();
r.Show();
ReportViewer v = r.Controls.Find("reportViewer1", true).FirstOrDefault() as ReportViewer;
v.LocalReport.ReportEmbeddedResource = "bolt.rptBolt.rdlc";
//Apply your ReportId to filter the record
//But it is good, to apply this filter to the original database call/query
lstCustomer = lstCustomer.Where(x=>x.Id==ReportId).ToList();
ReportDataSource dataset = new ReportDataSource("ReportDataSet1", lstCustomer);
v.LocalReport.DataSources.Clear();
v.LocalReport.DataSources.Add(dataset);
v.LocalReport.Refresh();
v.RefreshReport();
this.Hide();
private int GetTheSelectedRecordFromGridView()
{
//Write your logic, to get the selected Id;
}
Pass the reportId to frmReport, filter the record from database, build Report dataset with the desired fields, bind with reportviewer and display id, firstname and lastname fields.
Hope you got the logic / idea.

Refresh one row in Dataset from Database before show edit form

I have a DataSet + TableAdapter and bound dataGridView. I have an edit button which opens a new form with details to edit (WinForms). How do I refresh one row (selected one) in the Dataset and in dataGrid from database before opening the new form?
Example: Two users A and B. User A changed record ID(10) and user B still has the old value in record ID(10). User B presses edit button and should get fresh data from database (data after change made by user A).
string sql = "SELECT * FROM Orders";
SqlConnection connection = new SqlConnection(connectionString);
SqlDataAdapter da = new SqlDataAdapter(sql, connection);
DataSet ds = new DataSet();
connection.Open();
da.Fill(ds, "Orders");
connection.Close();
dataGridView1.DataSource = ds;
.....
private void button1_Click(object sender, EventArgs e)
{
//?
//refresh selected row in datagrid (from current database record)
//?
EditForm()
}
Create a new query in your TableAdapter that should only get one row and call it with just the primary key of the row you already have.
Plug the result into a new DataSet and grab the first row (after checking for Rows.Count() > 0)
I would enclose the populating the gridview bit in it's own method say something like fill_grid(). Then just call the method in the edit link click event. Then whatever has changed will update. Then add the method to all the other button link events too. Then every time the user is clicking a link the grid "refreshes" essentially.
For those who is looking for the answer in 2019,
assuming dataAdapter is a defined earlier class member
private void ButtonUpdate_Click(object sender, RoutedEventArgs e)
{
if (dataGrid.SelectedItem is DataRowView view)
{
DataRow currentRow = view.Row;
DataTable table = currentRow.Table;
dataAdapter.Fill(table.Rows.IndexOf(currentRow), 1, table);
}
}

Refresh button - Refreshing data grid view after inserting, deleting, updating

I'm trying to create a refresh button to automatically refresh the data inside my datagridview after i have finish updating them.
However, my refresh button doesn't seem to work. The data displayed remains the same as the original one. It only gets updated after i manually end my windows app and rebuild it.
Here is my code:
private void button_refresh_Click(object sender, EventArgs e)
{
this.acuzioSecureStore_DatabaseXDataSet.AcceptChanges();
}
Please assist. thanks ^_^
The easiest way to handle this is to use a Binding Source object.
If your loading information into your DataGridView from an Access Database then your most likely storing the Data in a Dataset or DataTable.
Create a Binding Source object, and once you have populated your DataTable/Dataset, set the datasource for your Binding Source to your DataTable. Then set the Datasource from the DataGridView as the Binding Source object.
Doing this ensures that any changes in your datagridview or reflected in the DataTable and vice Versa. If you reload data into your DataTable it will reflect in the Data Grid Automatically.
DataTable dt = new DataTable();
BindingSource bs = new BindingSource();
bs.DataSource = dt;
dataGridView1.DataSource= bs;
All changes will now happen automatically.
Hey the solution above is good but when the above code is executed,the table disappears and is not seen. And if I execute
da.Fill(ds, "p");
dataGridView1.DataSource = ds.Tables["p"];
then whole table is created again.
private void button_refresh_Click(object sender, EventArgs e)
{
SqlConnection con=new SqlConnection(#"");
string query="select * from abc";
SqlCommand cmd=new SqlCommand(query,con);
SqlDataAdapter da=new SqlDataadapter(cmd);
DataTable dt=new DataTable();
da.Fill(dt);
dataGridView1.DataSource=dt;
}
I had a datagridview, bound to a table in an Entity Framework database:
dataGridView1.DataSource = MyDatabase.MyTable;
It would never refresh despite two wasted days.
I solved it with a simple workaround:
private void button_refresh_Click(object sender, EventArgs e) {
dataGridView1.DataSource = MyDatabase.MyTable.Where(i =>(true));
}
This is an ugly workaround, and friend explained me how it works - if I do just dataGridView1.DataSource = database.table, it will cache the table and use the cached data forever. The fact that each time we create a new dummy query, prevents .net from caching it.
Please try this, it worked for me and let me know if you have any better option.
private void button3_Click(object sender, EventArgs e)
{
dataGridView1.Refresh();
}
you Can Bind dataGridView On PageLoad or UserControl Load Event and After chage in grid view Call the Load Event
like
this.ucUsers_Load(null, null); // Windows From C#

Categories