Devexpress : set column and row count to integer value - c#

basically want to set the column count and row count equal to 1 using devexpress.
//int startcolumn= 1;
//int startrow = 1;
//GridControl gd = new Gridcontrol();
//somefunction()
//{
//gd.colcount = startcolumn; //colcount & rowcount is a syncfusion type
//gd.rowcount = stratrow;
//}
how to achieve it in devexpress, please help, any similar property like colcount in devexpress or any other way to achieve it?

GridControl is not a matrix-editor. GridControl is a control for showing data. You can't just set number of columns and rows.
You can dynamically add new column:
DataColumn col = jobTable.Columns.Add("columnKey", typeof(string));
GridColumn column = gridViewJob.Columns.AddVisible(col.ColumnName)
column.Caption = col.Caption;
column.Name = col.ColumnName;
and add new row.

Related

DataGridView: custom cells destroyed when inserting/deleting/sorting rows

I have a grid where I need to use different cell types in the same column, based on the value of a field in the row.
This is a simplified version of the code I use to obtain these custom cells:
/* ... */
myGrid.DataSource = bs;
bs.DataSource = myDataSource;
bs.ResetBindings(true);
const int customCol = 5;
var rowsCount = bs.Count();
for (int r = 0; r < rowsCount; r++)
{
MyType row = (MyType)bs[r];
if (row.SomeVal == true)
{
var cell = new DataGridViewComboBoxCell();
cell.DataSource = MyList;
cell.ValueMember = "ID";
cell.DisplayMember = "Desc";
myGrid(customCol, r) = cell;
}
}
In this example myGrid is bound to a BindingSource, and has a column (column index = 5) that is a DataGridViewTextBoxColumn, so its cells are of type DataGridViewTextBoxCell, but for some specific rows I want cells to be of type DataGridViewComboBoxCell.
With this code the grid is correctly rendered, but as soon as I add or delete a row, or sort the grid, all my custom cells are reverted to standard ones.
I think that rather than alter cells after they are created, I should somehow intercept creation routine and alter their type as soon as they are created.
Unfortunately, I cannot figure out how to do this, as I can't find something like a "BeforeCellCreation" event.

Resize the table to fit the data grid view C#

I want my table size to fit the data grid view. The data grid view size is always the same but the table can change in amount of rows and columns.
Here is how I populate the table:
public DataTable createGridForForm(int rows, int columns)
{
// Create the output table.
DataTable table = new DataTable();
for (int i = 1; i <= columns; i++)
{
table.Columns.Add("column " + i.ToString());
}
for (int i = 1; i < rows; i++)
{
DataRow dr = table.NewRow();
// populate data row with values here
ListBox test = new ListBox();
myTabPage.Controls.Add(test);
table.Rows.Add(dr);
}
return table;
}
And here is how I create the datagridview:
private void createGridInForm(int rows, int columns)
{
DataGridView RunTimeCreatedDataGridView = new DataGridView();
RunTimeCreatedDataGridView.DataSource = createGridForForm(rows, columns);
//DataGridViewColumn ID_Column = RunTimeCreatedDataGridView.Columns[0];
//ID_Column.Width = 200;
int positionForTable = getLocationForTable();
RunTimeCreatedDataGridView.BackgroundColor = Color.WhiteSmoke;
RunTimeCreatedDataGridView.Size = new Size(995, 200);
RunTimeCreatedDataGridView.Location = new Point(5, positionForTable);
myTabPage.Controls.Add(RunTimeCreatedDataGridView);
}
I am getting an error when trying this code:
//DataGridViewColumn ID_Column = RunTimeCreatedDataGridView.Columns[0];
//ID_Column.Width = 200;
the error says that the:
Index was out of range. It may not be negative and must be smaller than the size
this is it
for the width-
RunTimeCreatedDataGridView.AutoSizeColumnsMode=System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;
and for the height --
totalRowHeight = RunTimeCreatedDataGridView.ColumnHeadersHeight;
foreach (DataGridViewRow row in RunTimeCreatedDataGridView.Rows)
totalRowHeight += row.Height;
If you add your datagrid to your tab control before get Columns[0] you don't get that error.
Your code should be like that:
DataGridView RunTimeCreatedDataGridView = new DataGridView();
myTabPage.Controls.Add(RunTimeCreatedDataGridView);
RunTimeCreatedDataGridView.DataSource = createGridForForm(rows, columns);
DataGridViewColumn ID_Column = RunTimeCreatedDataGridView.Columns[0];
ID_Column.Width = 200;
EDIT:
I add detailed lines to fill your DataGridView to it's container.
private void createGridInForm(int rows, int columns)
{
DataGridView RunTimeCreatedDataGridView = new DataGridView();
RunTimeCreatedDataGridView.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)));
myTabPage.Controls.Add(RunTimeCreatedDataGridView);
RunTimeCreatedDataGridView.DataSource = createGridForForm(rows, columns);
// fill the gridview to its container
DataGridViewColumn ID_Column = RunTimeCreatedDataGridView.Columns[0];
ID_Column.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
RunTimeCreatedDataGridView.Dock = DockStyle.Fill;
}
following line can be used to fill extra space with a specific column in a data grid view
RunTimeCreatedDataGridView .Columns["column_name"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
if you set this property of all column to DataGridViewAutoSizeColumnMode.Fill then all column takes same width

How do you make the header row unselectable in a table that was imported into a dataGridView

When the data loads into the dataGridView, if you click on the header column name of a particular column, it will re sort all the data in that column alphabetically. How do I make it so you can't do that?
This is the code which builds the individual dataGrids, each on its own tab.
var tabPage = new TabPage(name1);
DataGridView grid = new DataGridView(); //{ //Dock = DockStyle.Fill // };
grid.Location = new Point(10, 50);
grid.Size = new Size(950, 450);
grid.Name = "dg_" + name;
tabPage.Controls.Add(grid);
grid.ScrollBars = ScrollBars.Both;
grid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader;
comboBox1.Items.Add(name1);
tabControl2.TabPages.Add(tabPage);
loadData(name, grid);
If I understand you right, run this loop apter you populate your grid with data. THis will set the columns not sortable
foreach (DataGridViewColumn col in dataGridView1.Columns)
{
col.SortMode = DataGridViewColumnSortMode.NotSortable;
}
for datagridview there is a enumerator called DataGridViewColumnSortMode.by this you can sort out your problem
for (int m = 0; m <= dataGridView1.ColumnCount-1; m++)
dataGridView1.Columns[m].SortMode = DataGridViewColumnSortMode.NotSortable;
Refer this link

Multiselect property of Datagridview now selecting full row

I have a window form that has a datagridview. In this datagridview i am adding two columns dynamically as shown in below code.My problem is that when i click on these added columns ,Multiselect property is not selecting full row but when i click on first column of grid it selected full row.
if (gvlayoutload.Columns.Count == 0)
{
DataGridViewTextBoxColumn comboBoxColumnRInfo =
new DataGridViewTextBoxColumn();
comboBoxColumnRInfo.Name = "RowInfo";
comboBoxColumnRInfo.HeaderText = "";
comboBoxColumnRInfo.DataPropertyName = "RowInfo";
comboBoxColumnRInfo.ReadOnly = true;
comboBoxColumnRInfo.Width = 25;
comboBoxColumnRInfo.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
//comboBoxColumnRInfo.Frozen = true;
this.gvlayoutload.Columns.Add(comboBoxColumnRInfo);
DataGridViewTextBoxColumn comboBoxColumn =
new DataGridViewTextBoxColumn();
comboBoxColumn.HeaderText = "Row #";
comboBoxColumn.DataPropertyName = "RowNo";
comboBoxColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.NotSet;
comboBoxColumn.Width = 45;
comboBoxColumn.FillWeight = 45;
//comboBoxColumn.Frozen = true;
FRColumn col = new FRColumn();
col.Name = comboBoxColumn.HeaderText;
col.Type = 1;
col.Variable1 = variable1;
col.Variable2 = variable2;
col.Percent = Percent;
col.Rowno = Rowno;
col.Headersize = 8;
col.Bodysize = 8;
col.HeaderAlign = 0;
col.BodyAlign = 0;
lstcolumn.Add(col);
this.gvlayoutload.Columns.Add(comboBoxColumn);
When i click on RowInfo column or Row # column Multiselect property not selected full row.I have set multiselect property to true and multiselection mode is fullrowmode.
The DataGridView.SelectionMode property indicates how the cells of the DataGridView can be selected. The default value is RowHeaderSelect. The behavior of each mode is described in this MSDN link.
The first column in the DataGridView contains the row headers. If you select any cell in this column it will select the full row when the SelectionMode is RowHeaderSelect or FullRowSelect.
If you want to select multiple rows then set the DataGridView.MultiSelect = true.
You can also hide that column by setting DataGridView.RowHeadersVisible = false.

Unable to hide a column on CellClickEvent in C# datagridview

I have a DataGridView as shown in this link "http://i.stack.imgur.com/DgwaD.png".
OnCLick of Image in flags column I want to add a new row below the clicked row with only 2 columns which are "Start", "End", "Status" and "Flags" will be hidden for the newly added row.
DataGrid population is done as below:
CheckBox chkbox = new CheckBox();
chkbox.Text = "click";
//column 0
DataGridViewCheckBoxColumn checkcol = new DataGridViewCheckBoxColumn();
checkcol.HeaderText = "Select";
dataGridView1.Columns.Add(checkcol); // Want to hide on ImageClick event
//column 1
DataGridViewImageColumn imageCol = new DataGridViewImageColumn();
imageCol.HeaderText = "Package";
imageCol.Image = plus_img;
dataGridView1.Columns.Add(imageCol);
dataGridView1.Rows.Add();
//column 2
DataGridViewTextBoxColumn col2 = new DataGridViewTextBoxColumn();
col2.HeaderText = "Latest Update";
dataGridView1.Columns.Add(col2);
//column 3
DataGridViewTextBoxColumn col3 = new DataGridViewTextBoxColumn();
col3.HeaderText = "Installed Version";
dataGridView1.Columns.Add(col3);
//column 4
DataGridViewTextBoxColumn col4 = new DataGridViewTextBoxColumn();
col4.HeaderText = "Details";
dataGridView1.Columns.Add(col4);
Using CellContentClick:
int col = e.ColumnIndex; // clicked column index
int row = e.RowIndex; // clicked row index
dataGridView1.Rows[row + 1].Cells[0].Visible = false;
I am using last line to hide column 0 for next row. But it is giving me following error
"Property or indexer 'System.Windows.Forms.DataGridViewCell.Visible' cannot be assigned to -- it is read only". When I checked Read Only property for the grid and the column it is set to "false". Please help me with this.
To hidden a columns why don't you set Visible property of column directly?
dataGridView1.Columns[col].Visible = false
I am using last line to hide column 0 for next row.
Look like you expect hidden a cell and your code will not work. From MSDN the Visible property of DataGridViewCell are read only.
BTW, to hidden a cell you can work around by change Style of cell. Set Forecolor and background color has the same color and set ReadOnly = true to prevent editing.
Hope this help.

Categories