DataGridView variable formatting - c#

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
}
}

Related

Removing column based on the content of the cells

I need to delete the whole column in a DataGridView based on a certain condition. My DataGridView columns consists of numbers (1,2 and 3) the condition would be to delete all columns that does not contain (1,2 and 3). For Example if the column has only (1 and 3) it should be deleted.
How do I Overcome this? as many threads I went through only cover when you are certain of an index like below.
myGridView.columns.RemoveAt(index);
Some suggest hiding columns and thats not what I want, I want to completely remove the column from the DataGridView.
While it's better to do the job using data source of the DataGridView, but if the control is not bound to a data source, you can also use linq to get values from columns and check for some criteria, for example:
dgv1.Columns.Cast<DataGridViewColumn>()
.ToList().ForEach(c =>
{
var values = dgv1.Rows.Cast<DataGridViewRow>().Where(r => !r.IsNewRow)
.Select(r=>r.Cells[c.Index].Value).ToList();
if (!values.Contains(1)) //your custom criteria
dgv1.Columns.Remove(c);
});
Here is a really simple test. Load dgv1 this way:
dgv1.ColumnCount = 2;
dgv1.Rows.Add(1, 2);
dgv1.Rows.Add(3, 4);
Then use above code to delete columns and it just removes the last column.

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 set minimum row of table in rdlc or ssrs report?

Let say I want to set minimum row in the table, When my data is not full in the table then just insert blank row to full fill it.
This question looks so easy but it's really hard to find a solution to do it
So I need to share it here
1.In the table set header and 1 bound detail row
2.Insert blank row in the table as much as you want to fill the row (Outside Group)
3.Right click on the second row (the first is detail row) and click
right click and click Row Visibility note: You can input the next step code into Hidden Properties to but I found it not works properly so I recommend this way
4.Click Show or hide based on an expression then insert this code then OK
=IIF(CountRows() < 2, False, True)
5.Repeat the steps on every blank row and change condition number
Ex. the third row insert this code
=IIF(CountRows() < 3, False, True)
Ex. the fourth row
=IIF(CountRows() < 4, False, True)
What it does if your data has to use that row then it just hides the blank row and replace with your data
That it.
I post this because I hope to found this solution on the first minute when I was searching about this

Datagridview and checkboxlist

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 ++;
}

Add row to Datagridview with no columns

Is there a way to append 1 or 2 rows to the bottom of my Datagridview with no columns? My current DataGridView has 3 columns, but I want 1 or 2 rows at the bottom to "summarize" the above content. I've tried dataGridView1.Rows.Add("Test"); but that just adds "Test" to my first column in a new row. Is this possible?
Thanks!
The easiest solution would be to create a second DataGridView directly below the first. Then manually populate it with the single row that you want to be displayed with summary and to make it look like a single gridview, just make column header invisible for the second grid view using the following line
dataGridViewSummary.ColumnHeadersVisible = false;

Categories