i am looking for a way to show data that are Checked(1) in my Database to a DataGrid, excluding those UnChecked(0)..
Sample Database:
Fruits?
Apple - 1
Mouse - 0
Grapes - 1
Pencil - 0
Orange - 1
Button_Click
... on DataGrid display only checked Data / Rows
this will help you to add checkbox column in grid then make code for checked & unchecked as per your database. same way you can fetch the data on the WHERE condition of the checked row cell value...
DataGridViewCheckBoxColumn checkColumn = new DataGridViewCheckBoxColumn();
checkColumn.Name = "X";
checkColumn.HeaderText = "X";
checkColumn.Width = 50;
checkColumn.ReadOnly = false;
checkColumn.FillWeight = 10;
//if the datagridview is resized (on form resize) the checkbox won't take up too much; value is relative to the other columns' fill values
dataGridView1.Columns.Add(checkColumn);
Related
I am working with XamDataGrid where in i have to disable the Row:0 Cell:1 enabled all the times.
to achieve this i Set AllowEdit for the field to false and then dynamically onClick of the cell for selection: i am trying to get rowindex and cell index and see if they are for first row i set enable. But that enables entire column all rows which i dont want.
DataRecord selectedRecord = (DataRecord)frameSpacingDataGrid.ActiveRecord;
Cell selectedCell = frameSpacingDataGrid.ActiveCell;
CellCollection cellCollection = selectedRecord.Cells;
int indxSelectedCell = cellCollection.IndexOf(selectedCell);
var rowIndex = selectedRecord.Index;
if (rowIndex == 0 && indxSelectedCell == 0)
{
selectedCell.DataPresenter.IsEnabled= true;
}
Above code is written in SelectedItemsChanged event.
Also, when user deletes rows: new row:0 and cell:0 should be editable.
How do i achieve this Via Triggers/XAML/ Codebehind.
I have found a work around as stated here:
What i did is to set cancel for the edit event.
But surely this is not a proper solution.
I am using Synfusion's Data Bound Grid.
I have set two properties as
this.mygrid.TableModel.Options.EnterKeyBehavior = GridDirectionType.Right;
this.mygrid.TableModel.Options.WrapCellBehavior = GridWrapCellBehavior.WrapGrid;
If no column is hidden this works fine. If i hide any column then it doesn't works.
Please help me to achieve enter key behaviour if columns are hidden.
Thanks in Advance
I have handled the following code for hiding columns :
GridColHidden[] hiddenCols = new GridColHidden[ 3];
for (int i = 0; i < 3; i++)
{
hiddenCols[i] = new GridColHidden(i + 1);
}
this.gridDataBoundGrid1.Model.ColHiddenEntries.AddRange(hiddenCols);
I have now added the following code.
this.mygrid.TableModel.Options.EnterKeyBehavior = GridDirectionType.Right;
this.mygrid.TableModel.Options.WrapCellBehavior = GridWrapCellBehavior.WrapGrid;
I was able to see the first cell is set as current cell , when Enter key is pressed with the current cell's position placed in last cell in Grid.
I've read all the similiar posts about this darn control (DataGridViewComboBoxCell) and setting it's value programmatically but implementing all the suggestions hasn't worked. I could probably change the UI to get around this problem but I don't like to be beat!
private void PopulateAreaForRoleAssociation()
{
// If businessRoleList is null then no data has been bound to the dgv so return
if (businessRoleList == null)
return;
// Ensure businessArea repository is instantiated
if (businessAreaRepository == null)
businessAreaRepository = new BusinessAreaRespository();
// Get a reference to the combobox column of the dgv
DataGridViewComboBoxColumn comboBoxBusinessAreaColumn = (DataGridViewComboBoxColumn)dgvBusinessRole.Columns["BusinessArea"];
// Set Datasource properties to fill combobox
comboBoxBusinessAreaColumn.DisplayMember = "Name";
comboBoxBusinessAreaColumn.ValueMember = "Id";
comboBoxBusinessAreaColumn.ValueType = typeof(Guid);
// Fill combobox with businessarea objects from list out of repository
comboBoxBusinessAreaColumn.DataSource = businessAreaRepository.GetAll();
// loop through the businessRoles list which the dgv is bound to and get out each dgv row based upon the current id in the loop
businessRoleList.Cast<BusinessRole>().ToList().ForEach(delegate(BusinessRole currentRole)
{
DataGridViewRow currentRowForRole = dgvBusinessRole.Rows.Cast<DataGridViewRow>().ToList().Find(row => ((BusinessRole)row.DataBoundItem).Id == currentRole.Id);
// Get a reference to the comboBox cell in the current row
DataGridViewComboBoxCell comboBoxCell = (DataGridViewComboBoxCell)currentRowForRole.Cells[2];
// Not sure if this is necessary since these properties should be inherited from the combobox column properties
comboBoxCell.DisplayMember = "Name";
comboBoxCell.ValueMember = "Id";
comboBoxCell.ValueType = typeof(Guid);
// Get the business area for the current business role
BusinessArea currentAreaForRole = businessAreaRepository.FetchByRoleId(currentRole.Id);
// if the role has an associated area then set the value of the cell to be the appropriate item in the combobox
// and update the cell value
if (currentAreaForRole != null)
{
foreach (BusinessArea area in comboBoxCell.Items)
{
if (currentAreaForRole.Id == area.Id)
{
comboBoxCell.Value = area.Id;
dgvBusinessRole.UpdateCellValue(2, comboBoxCell.RowIndex);
}
}
}
});
}
The dgv is first bound to a binding list holding BusinessRole objects, then the combobox column is bound to a basic list of BusinessArea objects that come out of a repository class. I then loop through the bindinglist and pull out the row of the dgv that is bound to the current item in the bindinglist loop.
With that row I make a database call to see if the BusinessRole entity is associated with a BusinessArea entity. If it is then I want to select the item in the combobox column that holds the BusinessAreas.
The problem is that when the grid is loaded, all the data is there and the comboboxes are populated with a list of available areas, however any values that are set are not displayed. The code that sets the value is definately getting hit and the value I am setting definately exists in the list.
There are no data errors, nothing. It's just refusing to update the UI with the value I programmatically set.
Any help would be greatly appreciated as always.
Thanks
This code works for my first combo box I have in my row with the following code, but I try it with the next one in the row and it doesn't work. I could use the help with the rest I have 3 more to do. I do set the combo boxes on a second form from this form with the same code as is on the last line of the try block but using the cell information instead of the dataset
try
{
string strQuery = "select fundCode from vwDefaultItems where IncomeType = '" + stIncome + "'";
SqlDataAdapter daDefault = new SqlDataAdapter(strQuery, conn);
DataSet dsDefault = new DataSet();
daDefault.Fill(dsDefault);
strDefFund = dsDefault.Tables[0].Rows[0].ItemArray[0].ToString();
dgvCheckEntry.Rows[curRow].Cells[7].Value = dsDefault.Tables[0].Rows[0].ItemArray[0].ToString();
}
catch (Exception eq)
{
}
I have problem related to datagridview in Winform.
I have a list of table names in my left panel. When I click on Table I show the table content in right panel. I am showing data in a datagridview by fetching data and assigning datasource to dgv.
I am setting following property to dgv.
dgTemp.Dock = DockStyle.Fill;
dgTemp.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
dgTemp.AutoSize = true;
dgTemp.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
dgTemp.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
dgTemp.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
dgTemp.ReadOnly = true;
dgTemp.AutoGenerateColumns = true;
dgTemp.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dgTemp.AllowUserToAddRows = false;
My problem is there can be any number of columns in Datasource I am assigning to dgv. So if there are very few number of columns (say 1 or 2) the dgv size stands very small and the empty space on right side give very ugly look.
I can't use auto autosizecolumnmode to fill since when there is more columns all columns get shrink and expanding columns dont give me Scroll at bottom
so My requirement is
All space in datagridview should be filled. (should cover all area)
When there is more column, there should appear scroll, so it give better look
are there any events or properties which I can use ??
Thanks in anticipation.
Try this :
dgTemp.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
Update :
dataGridView1.FirstDisplayedScrollingRowIndex = 5; //use 5 because you want to start from 5
//you can have a horizontal scroll bar with this code :
dataGridView1.FirstDisplayedScrollingColumnIndex = 10; //you can choose every column you wanna start with that column
Update 2 :
int rows = dataGridView1.Rows.Count;
int columns = dataGridView1.Columns.Count;
if (rows < 5 && columns < 10)
{
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
}
else
{
dataGridView1.FirstDisplayedScrollingRowIndex = 5; //use 5 because you want to start from 5
//you can have a horizontal scroll bar with this code :
dataGridView1.FirstDisplayedScrollingColumnIndex = 10; //you can choose every column you wanna start with that column
}
I have a DataGrid bound to a matrix.
The main logic of the application is that any cell having a value of 0 will just display nothing (0 = no value in my referential).
Now what I want is that, when the user presses the Delete button, all the cells selected will be set to 0.
I can easily do that with one cell (because I keep track of the current row/column selected), but I can't get, let's say, a 2x2 square correctly.
The best way I figured out, using Binding magic, would be to get the row/column indexes of each cell selected, and then set the Binding's source to 0. But still, I can't have all of the row numbers (basically because grid.SelectedCells is an IEnumerable<DataGridCellInfo>, and I can only get the column with this object, I have no way to get the current row.)
It works when I only select one cell(or one row), by using the following:
DataGridRow dataGridRow = (DataGridRow)this.ItemContainerGenerator.ContainerFromItem(grid.CurrentItem);
How about multiple selection?
Any ideas here? Thanks!
Try this to get to the selected cells value:
DataGridCellInfo cell = dataGrid1.SelectedCells[0];
((TextBlock) cell.Column.GetCellContent(cell.Item)).Text = "";
Counting that the cells are textblocks.
foreach (DataGridCellInfo cellInfo in grid.SelectedCells)
{
DataGridColumn column = cellInfo.Column;
string propertyName = ((Binding)column.ClipboardContentBinding).Path.Path;
PropertyInfo pi = cellInfo.Item.GetType().GetProperty(propertyName);
if (pi != null)
{
pi.SetValue(cellInfo.Item, null, null);
}
}
ICollectionView cv = CollectionViewSource.GetDefaultView(grid.Items);
cv.Refresh();