Windows forms - display image in gridview - c#

I have to display an image in a GridView cell.
For example, let 'Stock' be a column in a GridView.
In that column, based on the condition, I have to display either an image or text. Is this possible?

Step 1 : Add your image in Resources.resx under properties folder. (ex. Picture1.jpeg)
Step 2 : Add a DataGridViewImageColumn in your DataGridView
Step 3 : Add image this way
for (int row = 0; row <= [YourDataGridViewName].Rows.Count - 1; row++)
{
((DataGridViewImageCell)gvFiles.Rows[row].Cells[1]).Value = Properties.Resources.Picture1
}
that's it
Manoj

I assume you mean DataGridView as GridView is a control in ASP.NET not Winforms.
And to answer your question: yes it is possible.
If you have DataGridViewImageColumn and want to show inside text you have to create textbox cell and replace it with default cell in that column:
var row = new DataGridViewRow();
row.Cells[Stock.Index] = new DataGridViewTextBoxCell();
row.Cells[Stock.Index].Value = "Test";

Related

DataGridView row header cell text visibility

I have a DataGridView in my WinForms application. Basically, I want to input some 2D data there or something like that. It means, that I want to have both columns named and rows named. And so I did. But then there was a problem. When it's fine with column names, the row names tend to be not visible. For example:
The code I use to somehow "beautify" the DataGridView:
private void BeautifyTable(TableView tableView)
{
foreach (DataGridViewRow row in tableView.Rows)
{
row.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
}
foreach (DataGridViewColumn col in tableView.Columns)
{
col.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
col.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
col.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
}
}
TableView is a class created by me, and it inherits from DataGridView.
So now my question would be: Is there a way to somehow make those row names/titles appear in a normal way (in this particular case those are: s0, s1, s2.., but they're like cut from the left side).
P.S Is there a good way to "stretch" the columns? I mean if I have f.e 10 columns they would fill the whole DataGridView width, but if I would add (I do this dynamically) 5, so there would be 15 columns, still they would fit, just the width of every column would decrease?
Increase the width of the row header:
dataGridView1.RowHeadersWidth =
dataGridView1.RowHeadersWidth * 2; // or another value
Try to hide those arrows by setting the RowHeadersVisible to False in the properties of the datagridview at the Form.cs [Design] or just simply add the code below.
Me.DataGridView1.RowHeadersVisible = False
I actually found the best answer by myself.
You can set the WitdthSizeMode also for row headers, like this:
tableView.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders;
And that's the result:

How do you add an image to a single specific cell in a DataGridView?

Using C# and Visual Studio, I have a DataGridView with 2 columns. For every row, the first column will display text. For every row EXCEPT one specific row, the second column will display text. In one specific cell in the second column, I need to show an image.
For example:
Row[0].Cell[0] = "test" Row [0].Cell[1] = "test"
Row[1].Cell[0] = "test" Row [1].Cell[1] = "test"
Row[2].Cell[0] = "test" Row [2].Cell[1] = need to display an image here
Row[3].Cell[0] = "test" Row [3].Cell[1] = "test"
There is more than one way to do it but here is a simple example that will set one single cell to show an image:
Bitmap bmp = (Bitmap) Bitmap.FromFile(someimagefile);
DataGridViewImageCell iCell = new DataGridViewImageCell();
iCell.Value = bmp;
dataGridView1[1, 2] = iCell;
Of course any other image source will work as well..
Try not to leak the bitmaps if you change them..

Select only a text of cell in datagridview c#

in a datagridview I want to select only the text of the cell and not the whole cell
if I put
Datagridview.CurrentRow.Cells.[Datagridview.CurrentCell.ColumnIndex].Selected=true;
the entire cell is selected
is there a way to select only the text of the cell like a vb6
Datagridview.SelStart=0
Datagridview.SelLength=Len(Datagridview.Text)
thanks for help
BeginEdit will select everything in the cell but you can specify the start and length of selection by:
((TextBox)dgv.EditingControl).SelectionStart = 0;
((TextBox)dgv.EditingControl).SelectionLength = 3;
You can do that by calling BeginEdit() method for the DataGridView control. It will start edit mode for the selected cell. You can also set the EditMode of the DataGridView control:
//set selected cell as you showed in your question
datagridview1.CurrentRow.Cells.[Datagridview.CurrentCell.ColumnIndex].Selected=true;
//call BeginEdit with true argument which will select all text within the cell
dataGridView1.BeginEdit(true);
//optionally set the EditMode before you call BeginEdit
dataGridView1.EditMode = DataGridViewEditMode.EditProgrammatically;
This work for me, after adding a new row i want to edit the 2nd column of the row being added.
at same time the value of the cell is being highlighted. Hope this helps"
DataGridViewCell cellQty = dgvBundle.Rows[dgvBundle.Rows.Count - 1].Cells[1];
dgvBundle.CurrentCell = cellQty;
dgvBundle.CurrentRow.Cells[1].Selected = true;
dgvBundle.BeginEdit(true);
To get the current cell programmatically:
string msg = String.Format("Row: {0}, Column: {1}",
dataGridView1.CurrentCell.RowIndex,
dataGridView1.CurrentCell.ColumnIndex);
MessageBox.Show(msg, "Current Cell");

How can I show independent buttons in a DataGridView?

I have a DataGridView with loaded elements from a database.
The problem is that I want to show a button in some rows (some elements need this button and some others don't) and I don't know how.
The only thing I have now is this code to show a 'delete' button but for every row.
DataGridViewButtonColumn btnDelete = new DataGridViewButtonColumn();
dataGridView.Columns.Add(btnDelete);
btnDelete.Text = "Delete";
btnDelete.UseColumnTextForButtonValue = true;
Thanks for the help!
You can do this by inserting DataGridViewButtonCells into the cells where you want the buttons.
For example:
var cell = new DataGridViewButtonCell();
cell.Value = "Button text"
cell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
dataGrid.Rows[rowIndex].Cells[columnIndex] = cell;
The column doesn't need to be a button column, it can be a textbox column, for example.
Or you could go the other way, make a button column and:
for (int i = 0; i < dataGrid.Rows.Count; i++)
if (i % 2 == 1) // whatever the condition is
dataGrid.Rows[i].Cells[COLUMN_WITH_BUTTONS] = new DataGridViewTextBoxCell();
You could also create a custom datagrid column and cell type (inherit from the ones which are the closest to what you need) which has a flag (that you add) that controls what is shown in the cell.
You don't need to do this immediately for the whole grid - you can change the grid at any time. For example, you can make a function that sets a cell to the appropriate type, then call it when something happens (in an event handler, etc).

Datagridview with multiple Tableadapters, view different colums for each view

I have a datagridview with multiple table adapters. every click on the dg exposes a new table on the same control.
How can i show different columns on click?
I've tried to use
DataGridViewColumn newCol = new DataGridViewColumn();
DataGridViewCell cell = new DataGridViewTextBoxCell();
newCol.CellTemplate = cell;
newCol.HeaderText = "numOfTexts";
newCol.Name = "numOfTexts";
newCol.Visible = true;
dg1.Columns.Add(newCol);
but it doesn't display the cell content, only the column name.
Thanks
You can use:
dg.AutoGenerateColumns = true;
See MSDN
true if the columns should be created automatically; otherwise, false. The default is true.
This is exactly what you need to display all columns of the table adapter.
You can hide the unnecessary columns.
Hope I helped...

Categories

Resources