How to checked row checked Janus Grid C# - 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());

Related

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 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";

Get checkbox value and store in IList

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"

Selecting a row in DataGridView programmatically

How can I select a particular range of rows in a DataGridView programmatically at runtime?
Not tested, but I think you can do the following:
dataGrid.Rows[index].Selected = true;
or you could do the following (but again: not tested):
dataGrid.SelectedRows.Clear();
foreach(DataGridViewRow row in dataGrid.Rows)
{
if(YOUR CONDITION)
row.Selected = true;
}
In Visual Basic, do this to select a row in a DataGridView; the selected row will appear with a highlighted color but note that the cursor position will not change:
Grid.Rows(0).Selected = True
Do this change the position of the cursor:
Grid.CurrentCell = Grid.Rows(0).Cells(0)
Combining the lines above will position the cursor and select a row. This is the standard procedure for focusing and selecting a row in a DataGridView:
Grid.CurrentCell = Grid.Rows(0).Cells(0)
Grid.Rows(0).Selected = True
DataGridView.Rows
.OfType<DataGridViewRow>()
.Where(x => (int)x.Cells["Id"].Value == pId)
.ToArray<DataGridViewRow>()[0]
.Selected = true;
<GridViewName>.ClearSelection(); ----------------------------------------------------1
foreach(var item in itemList) -------------------------------------------------------2
{
rowHandle =<GridViewName>.LocateByValue("UniqueProperty_Name", item.unique_id );--3
if (rowHandle != GridControl.InvalidRowHandle)------------------------------------4
{
<GridViewName>.SelectRow(rowHandle);------------------------------------ -----5
}
}
Clear all previous selection.
Loop through rows needed to be selected in your grid.
Get their row handles from grid (Note here grid is already updated with new rows)
Checking if the row handle is valid or not.
When valid row handle then select it.
Where itemList is list of rows to be selected in the grid view.
Try This:
datagridview.Rows[currentRow].Cells[0];
You can use the Select method if you have a datasource:
http://msdn.microsoft.com/en-us/library/b51xae2y%28v=vs.71%29.aspx
Or use linq if you have objects in you datasource
Try this:
DataGridViewRow row = dataGridView1.Rows[index row you want];
dataGridView1.CurrentRow = row;
Hope this help!

How do I pull data from a javascript populated table?

I am trying to pull data from my populated javascript table. How do I pull the value from a javascript row? I am using
for (var i = 0; i < rowCount; i++) {
var row = table.rows[i];
//This is where I am having trouble
var chkboxIndicator = row.cells[0].childNodes[1];
alert(chkboxIndicator);
//
if (chkboxIndicator == indicator && Checkbox.checked == false) {
table.deleteRow(i);
rowCount--;
i--;
}
}
which has an alert message of "undefined". I tried .value and .text as well and no progress. How do I get the text from the childNodes?
Since you're purportedly using jQuery, why not use it to get your checkboxes and simplify your life?
$("#mytableid tr").each(function() {
// find the checkbox (assuming you have only one; if not, use :eq(n))
var $checkbox = $("input:checkbox", this);
// if it's checked, delete this row
if ($checkbox.is(":checked"))
$(this).remove();
});
There are crazier ways to do it, but I thought that example would be illustrative. :)
Are you sure that chkboxIndicator is not null?
If it is null it will alert as undefined.
Try
for (var i = 0; i < rowCount; i++)
{
var row = table.rows[i];
// Add null check for row
if (row != null)
{
//This is where I am having trouble
// Add null check for cell
if (row.cells[0] != null)
{
var chkboxIndicator = row.cells[0].childNodes[1];
// Added null check for chkboxIndicator
if (chkboxIndicator != null)
{
alert(chkboxIndicator);
if (chkboxIndicator == indicator && Checkbox.checked == false)
{
table.deleteRow(i);
rowCount--;
i--;
}
}
else
{
alert('chkboxIndicator is NULL');
}
}
}
}
Depends on what your DOM actually looks like.
I suspect your problem is that childNodes[1] could be a text node.
You probably want to use a selector on the cell instead, such as:
$(row.cells[0]).find("input[type='checkbox']");
If you want it to be fast too, use jOrder (http://github.com/danstocker/jorder).
Create a jOrder table with your data. Make sure to have a field 'checked' in it with true or false values, and put an index on that column and the ID:
var table = jOrder(data)
.index('id', ['ID'])
.index('checked', ['checked'], { grouped: true });
Then, you can rebuild your grid with the unchecked rows removed using table.where([{ 'checked': true }]) as the source.
But first, populate your grid with the data from the jOrder table. Use table.flat() to obtain the flat table.
When you build the grid, bind the record IDs to the checkboxes e.g. by $('#elem').data(). When a checkbox gets (un)checked, update the 'checked' value in the table:
var before = table.where([{ 'ID': 5 }])[0];
var after = table.where([{ 'ID': 5 }])[0];
after.checked = true;
table.update(before, after);
Note that you shouldn't do this for each item on an "(un)check all" event though. In that case just simply pass on table.flat() or set the 'checked' values in the buffer returned by it, and then issue a table.reindex() and THEN call table.where(...).
The block of code that I was missing was the following
var row = table.rows.item(i).cells;
var cellLength = row.length;
var cellVal = row.item(5).innerHTML;
Thanks for leading me in the right direction

Categories

Resources