I can not find post that resolve my problem. I saw many times a Add method or Insert method for adding a row but I have not get them !! I work with .NET 4.5
I would like to show you a printscreen in order to demonstrate that I do not dispose these methods, but I can not because I am too novice to have this right :/
I need to create a gridview (not a datagridview) programmatically with one column and several rows. I populate rows manually. If I understand, I must proceed like this : create gridview, then a column that I add to gridview, then a row that I add to column, and then a tablecell that I add to row. Is that the good way to do ?
This is my code :
GridView listTypeBot = new GridView();
// Create my column
BoundField bf = new BoundField();
bf.HeaderText = "Types of bot available";
// Add my column to my gridview
listTypeBot.Columns.Add(bf);
// Create a row and a cell
GridViewRow gvr = listTypeBot.Rows[0]; // I have an exception here : out of range. When I watch my gridview, it does not have row.
TableCell tc = new TableCell();
tc.Text = name;
// Add cell to my row
gvr.Cells.Add(tc);
listTypeBot.Rows[0].Cells.Add(tc);
I think I get this exception because listTypeBot does not have any row. How can I add it please ?
Thanks in advance.
why not just use the DataSource to bind the data in your case the data source is the RSS ? and set AutoGenerateColumns to rue , something like that :
List<string> names = new List<string>();
names.Add(" the alghabban ");
GridView newGrid = new GridView();
newGrid.DataSource = names;
newGrid.DataBind();
and when you need to add row just add it to names and bind the Grid view again
Related
I am connected to a Database. I created an DataViewGrid which uses Databinding from that Database.
I want to use Rows and Columns as Headers, right now somehow there are just Columns as Headers.
I am using Visual Studio 15 and want to use the Designer.
Does anyone ever did that? Or know an better Windows.Form for creating Table, which are connected to the Database and have Headers at the Row and Column side?
Thx in advance
Your requirements are not very clear but I guess you want to customize the row and column headers. The dataGridView is probably one of the best controls to view data in Windows Forms. I will show how to do the following typical requirements would be;
To show column headers in Title Case or UPPER Case
Customize the column headers specific names
Show row numbers on row headers
Code below is in C#
//Show Columns in Title, Lower(textInfo.ToLowerCase) or Upper case textInfo.ToUpperCase
System.Globalization.TextInfo textInfo = new System.Globalization.CultureInfo("en-US", false).TextInfo;
foreach (DataGridViewColumn col in dataGridView.Columns)
{
col.HeaderText = textInfo.ToTitleCase(col.HeaderText);
}
//Customize specific column headers, using name or column index
dataGridView.Columns["number"].HeaderText = "No.";
dataGridView.Columns["quantity"].HeaderText = "Product Quantity";
dataGridView.Columns[0].HeaderText = "Code";
//Set Row headers with numbers
int rowNumber = 1;
foreach (DataGridViewRow row in dataGridView.Rows)
{
if (row.IsNewRow) continue;
row.HeaderCell.Value = "Row " + rowNumber;
rowNumber = rowNumber + 1;
}
dataGridView.AutoResizeRowHeadersWidth(
DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders);
Reference
Thirdparty Grids like DevExpress PivotGrid allow you do to sth. like this. The basic dataGridView is not able to present RowHeader as far as i know. You could create a custom column to your DataSource, which holds RowHeader information and show this as first column. If you modify the first columns layout a bit it looks like RowHeader maybe. Would be the only trick i know with basic dataGridView.
I am adding columns to datagrid and displaying in my application
// Create Datagrid
Mytable = new DataTable("My Table");
// Add Columns
Mytable.Columns.Add(My_Localization.Cultures.Resources.UserId);
Mytable.Columns.Add(My_Localization.Cultures.Resources.Name);
// Fill data
DataRow dr = Mytable.NewRow();
dr[0] = UserId;
dr[1] = Name;
// Add itemsource
DetailsDlg.ItemsSource = Mytable.AsDataView();
Now is it possible to change the columns names when my localization languages changes on the fly? Since columns are added in the code, I am not able to change the column headers. If I put the columns in wpf, then I am not able to add the data before assigning the itemsource. How do I solve this?
Assuming you are binding your datagrid from your code behind and you are not using MVVM.
You can always change your desire column in your code behind, like this:
DetailsDlg.Columns[0].Header = "New column name";
I'm trying to create some blank rows in datagridview. Unfortunately I'm unable to do this.
How can I implement blank rows in the grid?
I just implemented like this..
dgvGetData.AllowUserToAddRows = true;
dgvGetData.Rows.Add(5);
But things are not working....
Have you added the columns prior to creating rows? An error is thrown if you have not.
One simple way to go about adding empty rows is this. First you need to add the Columns.
//You can assign the Column types while initializing
DataGridViewColumn d1 = new DataGridViewTextBoxColumn();
DataGridViewColumn d2 = new DataGridViewCheckBoxColumn();
DataGridViewColumn d3 = new DataGridViewImageColumn();
//Add Header Texts to be displayed on the Columns
d1.HeaderText = "Column 1";
d2.HeaderText = "Column 2";
d3.HeaderText = "Column 3";
//Add the Columns to the DataGridView
DataGridView1.Columns.AddRange(d1, d2, d3);
//Finally add the Rows
DataGridView1.Rows.Add(5);
I'm using MySQL .net connector to fill a Datagridview using Mysqladapter and Datagridview.Bindingsource. That works good, but I want to change one thing:
In the table which is filled to the DataGridview, there is a column with a text type. The cells in this columns are displayed as a Datagridviewtextboxcell in the datagridview, but I want to change it to DataGridviewComboboxCell (the users should select between ~10 items).
I already tried a lot but nothing worked as it should. The Columns in the DataGridview are readonly, I cannot change DefaultCellTemplate to a DataGridviewComboboxCell, cause it doesn't inherit DataGridviewTextboxcell.
I also tried this: Gridview - convert textboxcell to comboboxcell and back and I think my problem could be solved over this way, but with this solution I have also 1 problem: It doesnt show a DropDown Button.
Any help will be greatly appreciated.
To do this you need to add a new DataGridViewComboBoxColumn to the grid and then hide the text box column.
I show this using code below but you can do the same using the designer (just set the properties I set in code using the designer).
The key things to note are:
DataPropertyName refers to a property of the grid's data source - likely your text box source
You need to provide the column with its own data source
DisplayMember and ValueMember refer to the data source of the column
Here is the code to add the column:
// Here I do this in the form constructor - there are other places you can do it
public Form1()
{
InitializeComponent();
DataGridViewComboBoxColumn col = new DataGridViewComboBoxColumn();
// You need to set some properties on the column to make it work
// Datasource is the source (usually a list) of objects to show in the combobox
col.DataSource = dataSource;
col.DataPropertyName = "ColumnInGridDataSource";
col.DisplayMember = "DisplayProperty";
col.ValueMember = "ValueProperty";
dataGridView1.Columns.Add(col);
// This hides the textboxcolumn
dataGridView1.Columns["YourTextBoxColumnName"].Visible = false;
}
In the answer you linked, before the line:
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex] = cb;
Try adding:
cb.DisplayStyle = DataGridViewComboBoxDisplayStyle.CHOOSE_ONE;
cb.FlatStyle = FlatStyle.CHOOSE_ONE;
I am not sure how exactly you want to style your comboboxes, so instead of "CHOOSE_ONE", try out the styles and pick the style you want.
Edit: Seems like you're not changing it to a combobox at all. Try this:
var values = new List<string> { "a", "b", "c" };
var cell = new DataGridViewComboBoxCell();
cell.DataSource = values;
dataGridView1[col, row] = cell;
Is there a way to add a customize DatagridviewRow using c#?
Ive made a class for the rows.
class CustomizeRow: System.Windows.Forms.DataGridViewRow
{
...
}
Now i can add some of my customize row
DataGridViewColumn column = new DataGridViewTextBoxColumn();
dataGridView1.Columns.Add(column);
CustomizeRow row = new CustomizeRow();
dataGridView1.Rows.Add(row);
But how can i handle when i am using binding source to add rows like this
bs = new BindingSource();
bs.DataSource = dataSet1.Tables["Table"];
dataGridView1.DataSource = bs;
You need to use DataGridView.RowTemplate.
From the MSDN page on the RowTemplate:
Setting the row template is necessary when external data is displayed
using custom types derived from the DataGridViewRow class. With
unbound DataGridView controls, you can programmatically populate the
Rows collection with instances of your custom row type. When
displaying external data, however, the rows are generated
automatically, but they are based on the row template, which you can
set to an instance of your custom row type.
So you can simply set the template row like so:
CustomizeRow row = new CustomizeRow();
dataGridView1.RowTemplate = row;