Get checkbox value and store in IList - c#

Need some help since I'm still a newbie in programming. I have a gridview that has five columns, the three columns composed of checkboxes. Then I need to get these checkboxes value if they were checked and put it in IList. My problem is how to implement it when adding it to my IList. Please help. See my code below. I know something is wrong.
public IList<Title> Checkboxes
{
get
{
CheckBox chkEPS;
CheckBox chkIMDF;
CheckBox chkPS;
IList<Title> checkedList = new List<Title>();
foreach (GridViewRow row in gvwTitles.Rows)
{
chkABC = (CheckBox)row.FindControl("chkABC");
chkABCD = (CheckBox)row.FindControl("chkABCD");
chkABCDE = (CheckBox)row.FindControl("chkABCDE");
if ((chkABC.Checked) && (chkABCD.Checked) && (chkABCDE.Checked))
{
checkedList.Add(new Title(What will be the value));
// how will I add the value, I am also considering what if the user check the chkABC checkbox, while the others were not checked??
}
}
return checkedList;
}
}
public Title(int id, bool _isPocketSharing, bool _isPreventSplitting, bool _isMissingDataFile)

I assume that you want to add title prperty on the basis of checkbox or checked or not
you can user ternary operator for this ? :
i think you need this
public IList<Title> Checkboxes
{
get
{
CheckBox chkEPS;
CheckBox chkIMDF;
CheckBox chkPS;
IList<Title> checkedList = new List<Title>();
foreach (GridViewRow row in gvwTitles.Rows)
{
chkABC = (CheckBox)row.FindControl("chkABC");
chkABCD = (CheckBox)row.FindControl("chkABCD");
chkABCDE = (CheckBox)row.FindControl("chkABCDE");
checkedList.Add(new Title(1 , chkABC.Checked ? true : false, chkABCD.Checked ? true : false, chkABCDE.Checked ? true : false));
}
return checkedList;
}
}

Try this:
checkedList.Add(new Title{name=1 ,name2=??,name3= ??, name4=??});//name,name1,name2,name..is suppose to your property of class "Title"

Related

How to checked row checked Janus Grid C#

I use janus gridex datagridview, In first column type is CheckBocx, I want to check if current row selected is checked or not, but my code not working
if(DBGrid.CurrentRow.Cells["CheckBoxColumn"].Value == true)
{
// do something
}
If the row is 'ActAsSelector = True' then you can use the row level checking itself :
if(DBGrid.CurrentRow.IsChecked)
{
// do something
}
Otherwise you need to cast the value to Boolean.
if((bool)DBGrid.CurrentRow.Cells["CheckBoxColumn"].Value == true)
{
// do something
}
If the row is 'ActAsSelector = True', you can use itself :
var CheckedList = string.Join(",",
gridEX1.GetCheckedRows()
.Cast<Janus.Windows.GridEX.GridEXRow>()
.Select(x => x.Cells["ID"].Text)
.ToArray());

Access to all checked RadGridView 's Rows via GridViewCheckBoxColumn (Telerik component)

i want to access all checked row in RadGridView(Telerik component) via GridViewCheckBoxColumn. Note : User select checkbox at run time ...
you can access checkbox column like this:
foreach (GridViewRowInfo item in radGridView1.Rows.ToList())
{
if(item.Cells[0].Value.Equals(true))
{
//dowhat you want
}
}
in "Cell[0]" the zero index is the index of checkbox column
Assuming you only have one checkbox column in your grid, you can use the following code to access that value. If you have more than one column of that type, you might want to do some sort of name check or perhaps bind the grid to a model.
Note: Make sure to check for null values.
foreach (Telerik.WinControls.UI.GridViewRowInfo rowInfo in reportGridView.Rows)
{
foreach (Telerik.WinControls.UI.GridViewCellInfo cell in rowInfo.Cells)
{
if (cell.ColumnInfo.GetType() == typeof(Telerik.WinControls.UI.GridViewCheckBoxColumn))
{
if (cell.Value != null && (bool)cell.Value == true)
{
// Some logic here to handle this ...
}
}
}
}

How to get datagridviewcheckboxcolumn value c#?

In my window form application I have a datagridview which has a checkbox column I want to retrieve its value is checked or not I attempted
if ((bool)dataGridView1[columnindex,rowindex].Value ==true )
{
MessageBox.show ("checked");
}
but it doesn't work
please answer me
This should works fine:
var value = dataGridView1[columnindex, rowindex].Value
So if you said "it doesn't work" - than just you need to provide us more information about error you got, etc.
You can try following code snippet
foreach (DataGridViewRow roow in dataGridView1.Rows)
{
DataGridViewCheckBoxCell chkchecking = roow.Cells[0] as DataGridViewCheckBoxCell;
if (Convert.ToBoolean(chkchecking.Value) == true)
{
}
}
DataGridViewCheckBoxCell checkbox = (DataGridViewCheckBoxCell)dataGridView1.CurrentCell;
bool ischecked = (bool)checkbox.EditedFormattedValue;
if (ischecked == true)
{
MessageBox.Show("True")
}
this code can get datagridview checkbox cell value

How to get IDs of only checked rows of a datagridview

I have a datagridview that contains list of subjects populated from Subject table from database.Columns include
Select(checkbox),
SubjectId,
SubjectName,
SubjectGroup.
Now I want if a user Selects on any of the desired rows, the corresponding SubjectId's should be added to a List. I have made and inserted into the desired table in the database.
The problem is that the new column of checkboxes I have added to this datagridview is not being detected.
My code is:
foreach (DataGridViewRow row in gvSubjectsOpted.Rows)
{
if (Convert.ToBoolean(gvSubjectsOpted.SelectedRows[0].Cells["SelectId"].Value=true))
{
olist.Add(gvSubjectsOpted.SelectedRows[0].Cells["SubjectId"].Value.ToString());
}
}
Late to the party. I had the same issue with trying to get the checkbox column by name, use the index instead. Here is a linq example assuming the checkbox is column 0 and the stored values for TrueValue and FalseVale are true and false respectively.
var checkedRows = from DataGridViewRow r in gvSubjectsOpted.Rows
where Convert.ToBoolean(r.Cells[0].Value) == true
select r;
foreach (var row in checkedRows)
{
olist.Add(row.Cells["SubjectId"].Value.ToString());
}
I realise this is an old post but I came across it and didn't think it was really answered in an efficient way so I thought I would add my method.
I have a similar block in my windows app. I read the values from the grid when the user clicks a button, and I want to know which rows they checked. As the checkboxes are in Cell 0 and the data I want is in Cell 1, I use the following code. Note the cast: it is important as it allows us the use the Where clause and therefore just a single line of code to get the collection of data. I could use the name of the cells instead of magic index numbers but then it would not fit your app so I put numbers instead (you should use names)
var checkedRows = dataGridView
.Rows
.Cast<DataGridViewRow>()
.Where(x => x.Cells[0].Value.ToString() == "1")
.Select(x => x.Cells[1]);
Note that this will give you an IEnumerable of type DataGridViewCell. If you want you can either add something like .Value.ToString() to the select or do this when you use your collection.
You question is similar to another SO question.
Check the answer of this Datagridview checkboxcolumn value and functionality.
Try this
foreach(GridViewRow r in gvSubjectsOpted.Rows)
{
GridViewCheckBoxColumn c = r.cells[0].Controls[0] as GridViewCheckBoxColumn;
if(c.Checked)
{
//Do something.
}
}
private void button1_Click(object sender, EventArgs e)
{
string subjId;
List<string> lines = new List<string>();
for (int i = 0; i < gvSubjectsList.Rows.Count; i++)
{
bool Ischecked =Convert.ToBoolean(gvSubjectsList.Rows[i].Cells["Select"].Value);
if (Ischecked == true)
{
subjId = gvSubjectsList.Rows[i].Cells["SubjectId"].Value.ToString();
lines.Add(subjId);
}
}
comboBox1.DataSource = lines;
}
//the most important thing is to set 'true' and 'false' values against newly added checkboxcolumn instead of '0' and '1'...that is,
CBColumn.FalseValue = "false";
CBColumn.TrueValue = "true";

How to check if checkbox in gridview is checked

I have a devexpress gridview, and in one of the columns is a checkbox. I want to check to see if the checkbox in that column is selected in the focused row, and if so, perform some action. How should I go about checking if the checkbox is checked?
bool value = (bool) gridView.GetRowCellValue(gridView.FocusedRowHandle, column);
If you are using DataBinding it is very easy. For example:
public class MyClass(){
public MyClass(){
}
public bool IsTrue
{
get{;}
set{;}
}
}
List<MyClass> manyMyClassObjects = new List<MyClass>();
//Add some values for sure
GridControl.DataSource = manyMyClassObjects;
Now the IsTrue property is binded to the Grid. The GridView just present the underlaying Data. If you change a value in the Grid it changes the value of your DataSource Object. This will work with any Property which implements a setter for sure.
DataRow[] rows = new DataRow[gvExcelSheet.RowCount];
for (int j = 0; j < gvExcelSheet.RowCount; j++)
{
rows[j] = gvExcelSheet.GetDataRow(j);
if ((bool)rows[j]["yourcheckboxcolumnname"] == true)
{
//your code
}
}
Simple as below:
bool value = Convert.ToBoolean(gvMain.SelectedRowsCount);

Categories

Resources