Save datagridview dynamically filled in C# - c#

I'm searchin for a way to solve my problem, in relation with a previous question.
I have to manage different payment methods in Datagridview.
An user (cashier for example) have to check all money they have at the end of their service.
In a parent form, i display payment methods names, automatically amounts from an other database, two empty columns and a column with buttons "details".
Initially the datagridview in parent form looks like this :
PAYMENT | QTY | AUTOMATIC | MANUAL | DIFF. | DETAILS |
_____________|_________|_____________|__________|__________|___________|
CREDIT CARD | 2 | 46.80 | | | [DETAILS] |
CHECK | 1 | 25.65 | | | [DETAILS] |
BANK TRANSFER| 1 | 150.25 | | | [DETAILS] |
When the user click on a details button, i display a child form with an other datagridview with details of a payment method. I load the form like this in the parent form :
var details = new FrmDetails("FormName", "IdOfPaymentMethod");
details.ShowDialog();
For the credit card for example, i get a datagridview in the child form like this :
DATE/HOUR | TICKET NUMBER | AMOUNT | CHECK |
_____________________|_______________|__________|_________|
01/11/2015 - 08:23 | 138170 | 12.90 | |
01/11/2015 - 09:45 | 138191 | 33.90 | v |
These are the two payments by credit card for the date that user has selected beforehand. The sum of both is what you can see in the column "AUTOMATIC" in parent form. The last column "CHECK" contains checkboxes, users checks if they have the payment with them.
In my example, user check only the second line with 33.90 and not the other one.
When they close this details form, the parent datagridview update like this :
PAYMENT | QTY | AUTOMATIC | MANUAL | DIFF. | DETAILS |
_____________|_________|_____________|__________|__________|___________|
CREDIT CARD | 2 | 46.80 | 33.90 | -12.90 | [DETAILS] |
CHECK | 1 | 25.65 | | | [DETAILS] |
BANK TRANSFER| 1 | 150.25 | | | [DETAILS] |
This is the simple operation of the application.
The problem is that I have to save each datagridview "details" in a SQL database (SQL Server). The user has to enter other information before these payments so the solution that I thought was to save the previous datas in a table "ENTRIES" then retrieve the ID max, and save each datagridview "details" with a link ID_ENTRY as a foreign key.
I can do that with a single datagridview "details" but i lose all the check/uncheck when i press on others details button on the parent form.
Is there a way to keep each datagridview "details" until the final recording?
I hope my message was clear and comprehensive.

The scope of each instance of FrmDetails is only the scope of the button's click event - hence the data is gone.
Either you'll have to keep around a collection of FrmDetails instances for each payment method row:
var details = new List<FrmDetails>();
foreach (var row in dataGridView1.Rows)
{
var detail = new FrmDetails("FormName", "IdOfPaymentMethod");
details.Add(detail);
}
// And when clicking on a Details button:
details[e.ColumnIndex].ShowDialog();
Or a collection of their equivalent data (I used List here, but I'd recommend something bindable - pending your source code):
var detailsData = new List<DetailsDataSources>();
foreach (var row in dataGridView1.Rows)
{
var details = GetThisRowsDetails();
detailsData.Add(details);
}
// And when clicking on a Details button, somehow set the DataSource:
var details = new FrmDetails("FormName");
details.childDataGridView1.DataSource = detailsData[e.ColumnIndex];
details.ShowDialog();
/*
* Ensure the changes made in the details form are then afterwards
* reflected in detailsData[e.ColumnIndex]
*/

Related

Don't know how i can select one option in dropdown excel and write in other cell c# excel

my excel and c# program is structured in this way, first the excel sheet is created that according to the data that comes from the db, a veryhidden sheet is created where the dropdown list data and the respective id of each dropdown object are , what I need is that in a new formula according to the chosen option I write the id of the chosen object in a separate column.
Currently only the objects of the dropdown list are shown
This is my c#
using (var rng = worksheet.Cells["A3:A60003"])
{
var list = rng.DataValidation.AddListDataValidation();
mycode...
list.Formula.ExcelFormula = String.Format("Types!$B$1:$B${0}", markets.Count() > 1 markets.Count().ToString() : "1");
}
An example of what I want is this
Column A ------------------------------------------------------------- Column B
| MarketID | Market |
| -------- | -------- |
| 1 | Option1 |
| 32 | Option4 |

C# query with count

I have a list of users having several properties and I want to know who and how had the same code (which is an user property)
For example :
user 1: Jacob, code 1
user 2: Charlie, code 1
user 3: Thomas, code 2
I need to query and have a result like this :
+--------+--------+
| Object | Count |
+--------+--------+
| user 1 | 2 |
| user 2 | 2 |
| user 3 | 1 |
+--------+--------+
"2" because 2 users have the same code and "1" because just one had the code
Can someone help me for this please?
You can get using this linq. change tablename and field name according to yours.
(from user in _context.Users
let count = _context.Users.Where(p=>p.code == user.code).Count()
select new {user.username, count = count}).ToList()
Let's try below code. I think it can help you well:
var result = mycontext.users.GroupBy(p=>p.User).Select(p=>new { Object = p.Key, Count = p.Count()});

Send Daily Emails via single nested loops function

I am creating a web page that will be run once a day to send email notices to supervisors. I want each supervisor to get a single email that contains data about all of their employees, of which might have multiple events.
I have some data like this:
supervisorEmail | employeeID | eventDate | eventDetails
===================================================================================================
george#blah.org | jones | 2014-03-18 | Watch a movie
george#blah.org | jones | 2014-03-20 | Convention registration
george#blah.org | smith | 2014-03-20 | Convention registration
george#blah.org | smith | 2014-03-20 | Convention registration
gayle#blah.org | nloya | 2014-03-13 | some other stuff
gayle#blah.org | nloya | 2014-03-25 | this and that
I start by retrieving all the above data and put into a DataTable.
Next I need to group by supervisor so each gets an email.
Within each email I need the rows filtered for that supervisor. I see how LINQ can be used for some of this. I'm lost at the moment for all of it. Could I get a shove in the right direction at least?
You can group the data by supervisorEmail:
var groups = data.GroupBy(d => d.supervisorEmail);
foreach(var g in groups)
{
SendEmail(g.Key, g); // g is an IEnumerable<some type> with the data for that supervisor
}
public void SendEmail(string supervisorEmail, IEnumerable<some type> data)
{
// send data to supervisor
}

how to structure data that will be fed into dropdown boxes?

I have denormalized data:
+----+----------+------+--------+
| pk | name | type | animal |
+----+----------+------+--------+
| 1 | alex | car | cat |
| 2 | alex | bike | cat |
| 3 | liza | car | dog |
| 4 | danielle | bike | dog |
| 5 | danielle | bus | dog |
+----+----------+------+--------+
I would like to have 3 dropdown boxes.
name
type
animal
after the user chooses the option for the first, there should be a cascade effect for the other dropdowns.
example: if the user chooses danielle for the name, the only two options for type would be bike and bus and the only option for animal would be dog
How should I structure the SQL tables? Should I denormalize?
I'd say that your solution depends on how much data do you have in this table.
If this table is relatively small, you could load it into memory, fill comboboxes by distinct values and then filter data by chosen field.
If it large, you maybe should denormalize your table as #astander says, fill comboboxes with data from reference tables and then when value changes, select filters from SQL like:
declare #name_id int -- input parameter, fill it with id of chosen name
-- filter for type combo
select distinct type_id from main_table where name_id = #name_id
-- filter for animal combo
select distinct animal_id from main_table where name_id = #name_id

Linq to Sql : how to get data - one to many

My app in ASP.NET C#. I use Linq to Sql, look the picuture
// 5 data talbe :
// Unit table
id | name | unit_type_id Unit_type
-------------------------
1 | Unit 1 | 1 id | name
2 | Unit 2 | 1 -------------
3 | Unit 3 | 1 1 | Type 1
4 | Unit 4 | 2 2 | Type 2
5 | Unit 5 | 1 3 | Type 3
6 | Unit 6 | 3
//Tool table // tool_type table
id | name | tool_type_id id | name
---------------------------- -------------------
1 | Tool 1 | 1 1 | Tool_type 1
2 | Tool 2 | 2 2 | Tool_type 2
3 | Tool 3 | 1 3 | Tool_type 3
4 | Tool 4 | 2
5 | Tool 5 | 3
//unit_tool table
unit_id | tool_id
---------------
1 | 1
2 | 1
1 | 2
1 | 3
2 | 2
2 | 3
3 | 3
3 | 2
I want to use linq to sql in C# to get data like
// Table data i want to get
Unit name | Unit type name | Tool name
-------------------------------------------------
Unit 1 | Type 1 | tool 1, tool 2, tool 3
Unit 2 | Type 1 | tool 1, tool 2, tool 3
Unit 3 | Type 1 | tool 2, tool 3
Unit 4 | Type 2 |
Unit 5 | Type 1 |
Unit 6 | Type 3 |
My code be low
// Create new class
public class unit_list
{
//Unit name
public string unit_name { get; set; }
// Unit type name
public string unit_type_name { get; set; }
// Tool name
public string tool_name { get; set; }
}
// Function get data
protected void show_unit()
{
// Create datacontext
for_testDataContext db = new for_testDataContext();
//Query data
IQueryable<unit_list> CourseList = from cl in db.units
select new unit_list()
{
unit_name = cl.name,
unit_type_name = cl.unit_type.name,
tool_name = // how to get data like "tool1, tool 2, tool 3"
};
}
You should be able to write something like this (assuming all the relationships are set up correctly in Linq):
select new unit_list()
{
unit_name = cl.name,
unit_type_name = cl.unit_type.name,
tool_name = string.Join(", ",
cl.unit_tools.Select(ut => ut.tool.name).ToArray()
)
};
In other words..
Get the one-to-many relationship to unit_tools
Select the corresponding row in tool and select the name column
Join to get a single comma-separated string
You don't have any foreign keys set up, which means you don't get automatic "navigation properties" with your mapped entities. You will have to either update your database and then regenerate the mapping, or add them yourself using the toolbox.
EDIT:
Okay, let's run down a very short checklist.
Are primary and foreign keys set and associated properly in the schema (your database)? This is important, as this is what the
sqlmetal executable uses to generate your object context.
Do the associations in your model point to the correct properties in the parent and child entities? You can check this by clicking on
an association between two objects in the model viewer and then expanding the Properties
pane. Expand the Child and Parent properties and verify they are not
empty.
If you've done everything right so far, clicking on the association between Unit_Tool and Unit should show a Child property called Units.
If you're missing an association, you can add one by:
Clicking on the Association tool in the Toolbox.
Clicking on the object which defines the primary key.
Clicking on the object that holds the foreign key.
This will bring up a dialog box that should be pretty self-explanatory.
HTH.
EDIT:
Solved this by playing around with your existing code (thanks for the upload). The problem was super simple, but not exactly obvious: you need to have an identifier column in each table, or Linq-to-SQL doesn't create the child collection reference in the data context. So, since you're using unit_tool as just a pivot table (perfectly acceptable), without a unique ID on each row, you don't get a "navigation property." To fix this, just add a primary key field (you don't ever have to actually use it - it's just for the ORM) to the unit_tool table, recreate the entity mapping, and viola!, your query will work as-is.
I have to say, this wasn't obvious at first blush. I haven't ever had this problem, since I always create a unique identifier out of habit...

Categories