checkbox column value in radGridview - c#

I'm working with radGridView and I'm building the data assigned to it, I mean I'm creating the columns and the rows (not assigning to datasource). After adding the data to the grid I add a checkbox column that should be used to check multi rows (same as muti-select idea but using checkboxcolumn),
so this column is not bind to any data, its value should be assigned during runtime.
When I try to retrieve this checkbox column value it turned out to be null even if it's checked, I used this method to update its value, but it didn't work out:
private void radGridView1_ValueChanged(object sender, EventArgs e)
{
if(radGridView1.CurrentCell!=null)
if(radGridView1.CurrentCell.ColumnInfo is GridViewCheckBoxColumn)
radGridView1.TableElement.Update(GridUINotifyAction.DataChanged);
}
What should I do to get the value of the checkbox column not on valueChanged but on another button click?

I am not sure how you are getting null value, however, in my tests the check boxes always have values if they are assigned:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
radGridView1.Columns.Add(new GridViewCheckBoxColumn() { Name = "CheckBoxCol" });
radGridView1.Rows.Add(false);
radGridView1.Rows.Add(true);
radGridView1.Rows.Add(false);
radGridView1.Rows.Add(true);
}
private void button1_Click(object sender, EventArgs e)
{
foreach (GridViewRowInfo row in radGridView1.Rows)
{
Console.WriteLine(row.Cells["CheckBoxCol"].Value);
}
}
}
Can you please provide a small sample and information how to reproduce the issue?

Related

Is there a way of change values within a DetailView from a selected it value from GridView?

I would like to display the correct object Customer in the DetailView. The problem is that because DetailView is created at the design time, it always displays the first Customer object from my table, not the selected one from the GriedView.
Does it exist something like this?
Thank you!!
protected void grdvReceipt_SelectedIndexChanged(object sender, EventArgs e)
{
if (grdvReceipt.SelectedIndex != -1)
{
//Display Customer in DetailView by updating the ObjectDataSource
this.dtvCustomerFullDetail.ObjectDataSource['id']=grdvReceipt.SelectedRow.Cells[7].Text;
}
}

How to get value of first column when selecting a row in datagridview c#

I want to store on a variable the value of the first column of a row whenever I click on a cell.
You can try this code :
private void MyDataGrid_CellClick(object sender, DataGridViewCellEventArgs e)
{
var val = MyDataGrid.Rows[e.RowIndex].Cells[0].Value;
}
Hope this help you.

How to get Row Id in RepositoryLookupEdit_ValueChanged event

I have gridcontrol that has a RepositoryLookupEdit in one of the columns. I can get the value of RepositoryLookupEdit after changed, but I dont know how to get the which row's RepositoryLookupEdit value changed. How can I get the Row ID?
With the code below, I can get the RepositoryLookupEdit value.
private void repositoryItemLookUpEdit1_EditValueChanged(object sender, EventArgs e)
{
LookUpEdit edit = sender as LookUpEdit;
var row = edit.Properties.GetDataSourceRowByKeyValue(edit.EditValue);
}
Since repositoryItemLookUpEdit isn't restricted to GridControls you cannot get the row handle from this event. You however have other possibilities.
First, if the edit is done by the user, you can use the ColumnView.GetFocusedRow() method to get the current grid row.
If however the edit value is changed via code it will also be changed in the grid so you can now use the ColumnView.CellValueChanged event.
private void repositoryItemLookUpEdit1_EditValueChanged(object sender, EventArgs e)
{
LookUpEdit edit = sender as LookUpEdit;
var row = edit.Properties.GetDataSourceRowByKeyValue(edit.EditValue);
gridRow = gridView.GetFocusedRow() as MyDataRow
}

C# reading datagridview from one form to another form

I have two forms, one contains a datagridview control with a data source from MS Access. Data is properly displayed on the datagridview. First I select a cell in that data grid view, then I obtain the Row Index of the currently selected cell. I use that Row Index in an accessor function (code is in the main form):
public String Name
{
get
{
return dataGridView1.Rows[selrow].Cells[1].Value.ToString();
}
}
selrow contains the row index of the currently selected cell. Next, I click a button "Edit record" and this it displays my 2nd form as a modal form. I want to display the value of the above accessor in a text box, so code goes like this (code is in the 2nd form):
private void EditRecord_Load(object sender, EventArgs e)
{
CashLoan main = new CashLoan();
txtEName.Text = main.name;
}
But when I try to run and debug, I get this "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index" directed at my accessor function. I can't seem to find the source of the problem. Thank you in advance.
If I understand you want show value from main form in the textbox of Editrecord form. If so
In Load handler you create new instance of main form(CashLoan).
And trying read values from empty datagridview(I assume you not filling datagridview with data in the contructor) of main form.
Try pass your value to EditRecord form as parameter in contructor:
In EditRecord form:
private String _originalValue;
public EditRecord(String originalValue)
{
_originalValue = originalValue;
}
//Then in Load handler
private void EditRecord_Load(object sender, EventArgs e)
{
this.txtEName.Text = _originalValue;
}
//In main form
EditRecord frm = new EditRecord(this.name);
frm.ShowDialog();
Try
public String Name
{
get
{
return dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[1].Value.ToString();
}
}
cause i guess you have a problem with the current selected row index. Also it would be possible that your row have only one cell. In that case replace the 1 with a 0 cause index starts at 0 (so 0 = 1, 1 = 2 etc...)
dataGridView1.Rows[selrow].Cells[0].Value.ToString();

How to transfer data from gridview to textbox

Suppose if I say I have selected a row in gridview and want that row to come up in to their respective textboxes. For example, I have selected a row which has First Name and Last Names and on selection of row, the data from gridview should come in to textbox on winform. Please tell me.
I am guess it is something based on selection changed event if I am right ? Like below:
private void dgv_SelectionChanged(object sender, EventArgs e)
{
string str = txtFirstName.Text;
DataGridViewRow selectedRow;
}
If I got your question right, your solution is to use DataGridView.SelectedRows Property along with DataGridView.SelectionChanged Event
DataGridView.SelectedRows Gets the collection of rows selected by the
user.
DataGridView.SelectionChanged occurs whenever cells are selected or
the selection is canceled, whether programmatically or by user action.
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count > 0)
{
foreach (DataGridViewRow row in dataGridView1.SelectedRows) {
//Send the first cell value into textbox'
Txt_FirstName.Text = row.Cells(0).Value.ToString; // or row.Cells["ColumnName"].Value;
}
}
}
MSDN and MSDN

Categories

Resources