Datagridview and checkboxlist - c#

I have a question. Let's say i have a checkboxlist with the ingredieints for a cake and i choose not just one but more ingredients (3-4). I want to save this in datagridview. Can datagridview show more than one ingredient or is this not possible ?
You see i'm making an application for bakeries and for ordering cakes you will choose the type of cake and adding(toppings) and the number of cakes you want. Then I would like to save this to datagridview, or show this in dgv and then when I would finish with ordering i would just click on send button in dgv and all the orders i made would be sent to an email. I hope you understand what i want.

Even in its simplest uses a DataGridView is a 2-dimesional container; a 'grid', so you can have as many rows and columns as you want.
If you want to display them all in row you should first try to estimate the maximum number of ingredients. Then you add this number of columns to the target DGV.
Then add a row and fill its column cells with the ingredients.
In case you come across a cake with even more ingredients that would't be a problem either as you can always add more columns.
Empty cells are no problem, just make sure to always check for null values.
By the way, cells are quite powerful and you can have a tag for each single cell, so you can add as many data of any complexity to the cell beyond its mere value!
Here is some simple sample code:
while (dataGridView1.ColumnCount < checkedListBox1.CheckedItems.Count)
dataGridView1.Columns.Add("", "");
int newRow = dataGridView1.Rows.Add();
int item = 0;
foreach ( object o in checkedListBox1.CheckedItems)
{
dataGridView1[item, newRow].Value = o.ToString();
item ++;
}

Related

How do I set a DataGridView to show only rows containing a specific value?

This one is pretty simple but, I'll go into detail. I need to make a dataGridView filter the contents of the database table it's referencing to only show rows where the "TechID" column is a specific value I'm feeding to it from another variable. Actually obtaining that value has not been an issue, but I'm unclear as to how I'd even start to filter the DataGridView.
Please hurry, this is for a project due this week.
Edit: I've tried plugging this one into google a few times already and can only find results that work off of row indexes, which is not what I'm looking for. I need to filter by a value within the rows themselves.
Second Edit: the DataGridView is populated using a DataTable from a database as a source. I connected it to this table via the properties winddow in visual studio if that makes any difference.
Edit: Ok, so I didn't post the code I had before because I knew it wasn't correct, but since you asked, here you go:
{//techValue is the ID of the technician of the row selected in dataGridView1. If I hide the TechID column, it no longer works.
int selectedrowindex = dataGridView1.SelectedCells[0].RowIndex;
DataGridViewRow selectedRow = dataGridView1.Rows[selectedrowindex];
int techValue = Convert.ToInt32(selectedRow.Cells["TechID"].Value);
//This hides the rows that don't share a TechID value with the selected row in the dataGridView1.
/*int techValue2 = Convert.ToInt32(dataGridView2.Cells["TechID"].Value);*/
}```

c# add objects to table rows dynamiclly

I am trying to do something like this but I don't know how:
I have a table in c# which I increase and decrease during runtime.
Each row contains data, that is being parsed from an object.
I want to get an access to the object that the line is parsing, some a pseudo of it would be:
if (table.row.object == selectedObject)
I can't do it from the row number or something because the rows are being removed and inserted, each time with different objects.
any ideas, please?
EDIT: I am using TableLayoutPanel to work with a table, and this is how I add rows and removes them:
I am adding rows to the table dynamically, I have set of buttons and when I click one it deletes all old rows from the table and adds new rows that are related to this button.
This is the way I add new rows:
tlp.RowCount++;
tlp.RowStyles.Add(new RowStyle(SizeType.AutoSize));
tlp.Controls.Add(new Label() { ... }, cellIDX, rowIDX);
// adding more columns //
and to remove old rows I loop through all rows from bottom to top, removes all related controls of the current cell, then I remove style and row num like so:
tlp.RowStyle.RemoveAt(rowNum);
tlp.RowCount--;
thank you
you can also see a previous question I had related to this subject:
C# change table row color when mouse hover
You can try having a new column in the table (call it original_row_no) and set the row values into that, before you start your insertion/deletion process. That way you can reference the row directly (this is in response to your final line which says "I can't do it from the row number or something because the rows are being removed and inserted, each time with different objects."
A better way is to have a unique id in the table and run your references against that.
It is much more easier to help if you show the code you are trying.

How to select Row without key

There are many ways to select a row programmatically e.g. by if we know row index, or if we know the Primary-key-like column (column that contain unique value).
Now, when I'm creating my custom control, I wonder is there another way to select row using another way?
Let's say I backup the selected record in each OnSelectionChanged event using variable named BackupRow.
then when I sort the data using columnheader click, in OnSorted I want to select the grid using the BackupRow.
Is something like that possible?
Thank you.
No other method.. For solution to your problem you could.
Backup indices(in an integer list or array), no need to backup complete rows.. if you do need you do it as well. But you need to Backup indices any case.. And select rows form sorted grid on the base of those (backup) indices
if your data count never change and you can exactly know sorting algorithm its possible but not trustful for the true value..
as a pseudo-example : You have rows A-B-C-D and current is B..
if rows sorted ascendant (A to Z or 0 to 100) and then you sorted descendant(Z to A or 100 to 0) then its simply :
suppose your,
total rows = 45;
current row index for ascendant sort = 30;
sort descendant();
your current's new index = (total row - ascendant sorted row index)-1;
//-1 is for index [0] correction
your current's new index = 45-30 -1;
your current's new index = 14;
And an alternative way :
implement your own sort algorithm and create a delegate / pointer - i can't imagine which can be better right now-
then while each sort operation investigate your selected row and select again..
but as i said these are nost trustful ways..and noone never prefer / suggest..
Another Opinion :
Your question is not clear for me..this opinion is for clearer the possible misunderstood..
if you want "when user selects a row or sorts from-on a column value, the complete row cells get highlighted automatically- shown to user as Selected"
then its under datagridview's SelectionMode Property..
dataGridView1.SelectionMode = FullRowSelect;

DataGridView variable formatting

I have a DataGridView with data I extract from SQL Server.
I never know how many records i'll have in it so I need the grid to be variable to the number of rows extracted.
Exemple :
Picture 1 : What I have right now. 1 row was extracted so I have a big dark gray space where the could have been data.
Picture 2 : What I want to have with only 1 row extracted. The grid resizes automaticly depending on the number of rows.
Picture 3 : What I would like to have with many rows.
Also, I would like to remove the empty row that is always added too, adding a new row won't be an option.
Is that possible ?
You are looking for DataSet.Datatable.Rows.Count this will tell you the total rows returned from your Sql Server
Update this cannot be achieved by using the any properties of the DataGridView Control
You need to set the AutoSizeColumns properties:
datagridview.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
datagridview.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.DisplayedCells);
Assuming you're filling the grid from a DataSet, you can use this to prevent empty rows from being added:
foreach (DataRow row in dataset.Tables[0].Rows)
{
if (!string.IsNullOrWhiteSpace(row[0].ToString()))
{
//add row
}
}

Rearranged columns in C#

How can I get the rearranged columns out of a datagridview in C#? I need to get all the columns currently being displayed in order. I can get the default columns order by setting datagridview.AutoGenerateColumns=false but I get the same old order even after I've rearranged the columns in my table.
Edit: Basically what I need is to be able to iterate through all the columns of a datagridview based on their display index
Thanks
Use the DisplayIndex property of your DataGridView Columns.
The DisplayIndex property:
Gets or sets the display order of the
column relative to the currently
displayed columns.
Edit:
It's ugly, and I'm sure people are going to point and laugh, but this might give you a rough idea on one implementation to improve upon.
Create a DGV with three columns and set their DisplayName properties like so:
dataGridView1.Columns[0].DisplayIndex = 1;
dataGridView1.Columns[1].DisplayIndex = 2;
dataGridView1.Columns[2].DisplayIndex = 0;
Add the following code to loop through all the columns access the columns in the DGV in their DisplayIndex order:
for (int index = 0; index < dataGridView1.Columns.Count; index++) {
foreach (DataGridViewColumn c in dataGridView1.Columns) {
if (c.DisplayIndex == index) {
// You've got your next column.
Console.WriteLine(c.Name);
}
}
}
How are you selecting? SELECT *? You need to specify the columns in the order you want them.
Have you rearranged their order in the ItemTemplate section of your grid? You can also change the order via the context menu for a GridView, alternative you can change your SQL statement and make sure autogenerate is off

Categories