I'm working on a Windows Forms project and I have some data in a XtraGrid.GridControl with these columns:
ID, Description, To Process
I'm loading these data from database, and the column To Process contains a boolean field.
I would like to have instead of the current 1 and 0 values a checkbox, that will be checked if the value is 1 and unchecked if the value is 0.
How can I achieve this with Dev Express 16?
This is what I did so far:
imported a DevExpress.XtraGrid.GridControl in my form Design;
added three columns ID, Description and To Process;
populated the GridControl DataSource from code behind with this method:
private void LoadTableData()
{
// initialization
gcTable.DataSource = null;
string query = " SELECT id, description, to_process FROM test_table ";
DataTable dt = Utils.ExecuteQuery(query);
if (dt != null && dt.Rows.Count > 0)
{
gcTable.DataSource = dt;
}
}
As now, I have my table populated but with 1 and 0 values in the column To Process.
Assign a RepositoryItemCheckEdit to your To Process column's ColumnEdit property and set its ValueChecked and ValueUnchecked properties if necessary. More information here.
You can use gridView1_CustomRowCellEdit event to change repository for a cell of grid view. If data column is type of bool, cells of grid control will be check boxes.
dt.Columns["to_process"].DataType = typeof(bool);
Here is a sample code for CustomRowCellEdit event.
void gridView1_CustomRowCellEdit(object sender, DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs e)
{
if (e.Column.FieldName == "to_process")
{
DevExpress.XtraEditors.Repository.RepositoryItemCheckEdit repChk = new DevExpress.XtraEditors.Repository.RepositoryItemCheckEdit();
e.RepositoryItem = repChk;
}
}
Related
In my form I have datagrid view and I select the data into datagridview by using the following
stored procedure :
create proc [dbo].[GET_CULTURE_RESULT_DETAILS]
#ORDER_ID int
as
select LAB_MICRO_RESULTS_DETAILS.[organism_id] as 'Org.Id' , organism_desc as 'Organism Name'
,LAB_MICRO_RESULTS_DETAILS.[Antibiotic_id] as 'Ant.Id', Antibiotic_Name as 'Antibiotic Name'
,LAB_MICRO_RESULTS_DETAILS.[sensitivityId] as 'Sens.Id', Sensitivity_desc as 'Sensitivity Name'
from LAB_MICRO_RESULTS_DETAILS
inner join lab_organisms on LAB_MICRO_RESULTS_DETAILS.organism_id = lab_organisms.organism_id
inner join lab_antibiotics on LAB_MICRO_RESULTS_DETAILS.Antibiotic_id = lab_antibiotics.Antibiotic_id
inner join Lab_Sensitivity on LAB_MICRO_RESULTS_DETAILS.sensitivityId = Lab_Sensitivity.sensitivityId
where LAB_MICRO_RESULTS_DETAILS.order_id = #ORDER_ID
Then when select the order number datagridview filled using key down event this is the code :
private void txtOrder_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter && txtOrder.Text != string.Empty)
{
dgvcultures.DataSource = micro.GET_CULTURE_RESULT_DETAILS(Convert.ToInt32(txtOrder.Text));
txtPCfile.Focus();
}
}
Finally the error appeared when I try to add new row into the datagrid view by using this code :
private void btnAdd_Click(object sender, EventArgs e)
{
if (dgvcultures.Columns.Count >= 0)
{
dgvcultures.ColumnCount = 6;
dgvcultures.Columns[0].Name = "Org.Id";
dgvcultures.Columns[1].Name = "Organism Name";
dgvcultures.Columns[2].Name = "Ant.Id";
dgvcultures.Columns[3].Name = "Antibiotic Name";
dgvcultures.Columns[4].Name = "Sens.Id";
dgvcultures.Columns[5].Name = "Sensitivity Name";
dgvcultures.Rows.Add(comboOrganism.SelectedValue.ToString(), comboOrganism.Text,
comboAntibiotics.SelectedValue.ToString(), comboAntibiotics.Text,
comboSensitivity.SelectedValue.ToString(), comboSensitivity.Text);
// comboGrowth.SelectedValue.ToString(),comboGrowth.Text);
}
}
when click add button and try to insert new row I got this error :
ColumnCount property cannot be set on a data-bound DataGridView control. c#
I checked the solutions in this site and they said you have to clear the datagridview before insert new rows I tried it its clear the filled data from stored procedure and I dont need to clear datagridview , I need to keep the data and add new rows without clear the current data :
//Clear the binding.
dgvcultures.DataSource = null;
this command will remove the rows in datagridview , but I need to keep rows.
How to solve this error please.
From what I can decipher, the grids DataSource looks like a DataTable with the following columns…
Org.Id, Organism Name, Ant.Id, Antibiotic Name, Sens.Id and Sensitivity Name.
I assume this from the first snippet of code that gets the table from the DB.
If this DataTable is used as a DataSource to the grid, then in the button click event, it is unnecessary to “re-create” the columns. Those columns already exist in the table. The code simply needs to add the row to the DataTable. It is not clear if the column “types” are all strings and the code may have to convert some values, otherwise the code in the button click event may look something like…
DataTable gridDT = (DataTable)dgvcultures.DataSource;
gridDT.Rows.Add(comboOrganism.SelectedValue.ToString(),
comboOrganism.Text,
comboAntibiotics.SelectedValue.ToString(),
comboAntibiotics.Text,
comboSensitivity.SelectedValue.ToString(),
comboSensitivity.Text);
Lastly, you should consider using a BindingSource. It may make things easier.
I hope this makes sense.
I want to create a form which I can open tables within different database and delete some lines.
looks like this. the tables are different. so I use
boxGrid.Controls.Clear();
DataGridView g = GetTableGrid(databaseName);
boxGrid.Controls.Add(g);
GetTableGrid() will generate a new DataGridView and replace the one before
it works will on changing table. but causes problems for me to get the selected row in the table to delete.
What can I do?
I only need to get the first col in the row. It is the primary key in the database.
the problem here is that you are always creating a new datagridview whenever the user selects a new table (i would assume its the buttons below the datagridview)
What you can do is before adding the datagridview on your boxgrid object, you can add an SelectionChanged to it.
boxGrid.Controls.Clear();
DataGridView g = GetTableGrid(databaseName);
g.SelectionChanged += new EventHandler(dvg_SelectionChanged);
boxGrid.Controls.Add(g);
and then you can get the value you are looking for like this
private void dvg_SelectionChanged(object sender, EventArgs e)
{
DataGridView dvg = (DataGridView)sender;
//Check first if datagridview has data and
//Check if you are selecting a valid row
if (dvg.Rows.Count > 0 && dvg.CurrentCell.RowIndex > 0)
{
int index = dvg.CurrentCell.RowIndex;
DataGridViewRow row= dvg.Rows[index];
string mykey = Convert.ToString(row.Cells["columnName"].Value);
//Or you can store the information you've got here to some
//Variable you can use to open the form you want.
}
}
If you're only trying to get a certain column (in this case, the first one) of a clicked row you can do the following (it's how I do it):
private void yourGridView_SelectionChanged(object sender, EventArgs e)
{
//makes sure a row is selected
if (yourGridView.SelectedCells.Count > 0)
{
int selectedrowindex = yourGridView.SelectedCells[0].RowIndex;
DataGridViewRow selectedRow = yourGridView.Rows[selectedrowindex];
//guessing you´re storing your value in a variable
string a = Convert.ToString(selectedRow.Cells["columnName"].Value);
//where columnName is the name of the column you want the value printed of...
}
}
I am having below code that successfully adds a check box column to the ultrawingrid, my problem is that when I make a selection by checking a check box on the Select column the count of selected rows of ultrawingrid is not updated and it still shows the count as zero, and also I want to know how to enable multi checkbox selection i.e multiple rows selected...
Below is the code...
private void grdPayVis_InitializeLayout(object sender,InitializeLayoutEventArgs e)
var gridBand = grdPayVis.DisplayLayout.Bands[0];
if(!gridBand.Columns.Exists("Select"))
gridBand.Columns.Add("Select", "Select");
gridBand.Columns["Select"].Header.VisiblePosition = 0;
gridBand.Columns["Select"].Hidden = false;
gridBand.Columns["Select"].Style = Infragistics.Win.UltraWinGrid.ColumnStyle.CheckBox;
gridBand.Columns["Select"].AutoSizeMode = ColumnAutoSizeMode.AllRowsInBand;
gridBand.Columns["Select"].CellClickAction = CellClickAction.Edit;
}
I am not sure what you actually is trying to achieve. When you set CellClickAction in your Select column to Edit, and you click on any cell in this column the grid will select the cell and not the row. Grid has Selected property which exposes three collections - rows, columns and cells. In your case you are changing the selected cells and do not change the selected rows collection. If you need to select rows you need to set CellClickAction in your Select column to RowSelect. If you need in the same time to shange the checkbox state of the Select column you may handle AfterSelectChange event of the grid like this:
private void grdPayVis_AfterSelectChange(object sender, AfterSelectChangeEventArgs e)
{
if (e.Type == typeof(UltraGridRow))
{
foreach (UltraGridRow row in this.grdPayVis.Selected.Rows)
{
row.Cells["Select"].Value = true; // Or some value appropriate to your scenario
}
this.grdPayVis.UpdateData();
}
By default, the grid allows you to select many cells, rows or columns. However, when there is a cell in edit mode you cannot select any other cells. So again, as you have set CellClickAction to Edit when you click any cell in Select column it enters edit mode and no more cells could be selected before you exit edit mode.
You can add a new column to your datatable AFTER reading it from the database
ds = new DataSet("PayCharge");
ds= obj.GetData();
DataColumn dc = new DataColumn("Select", typeof(boolean));
dc.DefaultValue = false;
ds.Tables[0].Columns.Add(dc);
grdVisPay.SetDataBinding(ds, "PayCharge");
.....
Now the first table in your dataset has an unbound column named Select and when you set that as your datasource you will be able to manipulate as you have already done. But this time whenever you touch the checkbox the value True/False will be set in the underlying datatable. When you need to discover what are the checked rows you work with the datasource, not with the grid. For example
void buttonSave_Click(object sender, EventArgs e)
{
DataSet ds = grdVisPay.DataSource as DataSet;
DataRows[] selectedRows = ds.Tables[0].Select("Select = True");
foreach(DataRow row in selectedRows)
{
... do whatever you need with the selected row...
}
}
How to bind a multiple rows to report in label or table ?? If my table is single row I can easily bind and it display in label and also display in table but my table consist of 2 or 3 rows it wont display. How to show multiple row in a report ? I need o filter it according to which item is selected ??
Don't bind it directly. Use BeforePrint event for that label (not for the report!)
private void xrLabel4_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e) {
var id = this.GetCurrentColumnValue<int>("ID"); // get value of field ID for this row
var lines = GetRows(id); // for demo only: returns string[]
(sender as XRLabel).Lines = lines;
}
Don't forget to set property Multiline to true.
I have a datagridview with 4 columns. In first column,there is a textbox and the value is coming from the checkedlistbox ie outside the datagridview. Now in second column there is a combobox and in that value is manually filled. And in third column there is textbox and i'll fill the value and after that in fourth column,the value will be (column2 * column 3) ie automatically come after the calculation. So how can i fill value automatically in column fourth. please help me.
To make it dynamically change, you should handle some event, also make some init method to initialize the grid:
//CellValueChanged event handler for your dataGridView1
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e){
var col = dataGridView1.Columns[e.ColumnIndex];
if(col.Name == "column2" || col.Name == "column3") {
//update the column4
dataGridView1[dataGridView1.Columns["column4"].Index, e.RowIndex].Value =
dataGridView1[dataGridView1.Columns["column2"].Index, e.RowIndex].Value *
dataGridView1[dataGridView1.Columns["column3"].Index, e.RowIndex].Value;
}
}
//use this method to initialize all the values for the `column4`, this will be done only
//once in ,for example, your Form constructor
private void InitColumn4(){
foreach(DataGridViewRow row in dataGridView1.Rows){
if(row.IsNewRow) continue;
row.Cells["column4"].Value = row.Cells["column2"].Value * row.Cells["column3"].Value;
}
}
//call it in your form constructor
public Form1(){
InitializeComponent();
//init data for your grid first
//...
InitColumn4();
}
NOTE: I suppose your columns have names "column2", "column3", "column4" respectively. Using column name is better because you may change your column index unexpectedly and your code will be broken and the code is also more readable.