Get value of rows when it s checkbox is checked in radgrid - asp.net

I have a radgride with checkbox in each rows.I want when i click on button,it checks id of each rows that checked,put into the variable..
I know this code for gridview but I do not know equivalent of this code for radgrid
foreach (GridViewRow rw in grid1.Rows)
{
CheckBox chkBx = (CheckBox)rw.FindControl("Check");
if (chkBx != null && chkBx.Checked)
{
id= grid1.Rows[0].Cells[3].Text.ToString();
}
}

if (RadGrid1.Items.Count > 0)
{
foreach (GridDataItem item in RadGrid1.Items)//loops through each grid row
{
CheckBox chkBx = (CheckBox)rw.FindControl("Check");
if (chkBx != null && chkBx.Checked)
{
//string v= item["ColumnUniqueName"].Text;
string v= item.Cells[3].Text; //accessing cell using its ColumnUniqueName
}
}
}
For more information on Radgrid rows and cell access, check http://www.telerik.com/help/aspnet-ajax/grid-accessing-cells-and-rows.html

Try this: http://www.telerik.com/community/forums/aspnet-ajax/grid/loop-through-radgrid.aspx
foreach(GridItem rw in grid1.Items)
{
CheckBox chkBx = rw.FindControl("Check") as CheckBox ;
if (chkBx != null && chkBx.Checked)
{
string id = rw.Cells[3].Text;
}
}

Related

Why column count in gridview is 0 when columns are binded dynamically from datatable

I have a gridview which show column count 0 when gridview is directly binded to datasource and columns are not defined statically. I am unable to come up with this problem.
I tried this below code:
foreach (GridViewRow row in gv_services.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
for (int i = 0; i < gv_services.Columns.Count; i++)
{
if (row.Cells[i].Controls[0].GetType() == typeof(CheckBox))
{
CheckBox checkBox = row.Cells[i].Controls[0] as CheckBox;
checkBox.Enabled = true;
// checkBox.CheckedChanged += new EventHandler(chck_CheckedChanged);
}
}
}
}
If i understood correctly you are trying to loop through gridview rows and enable checkbox based on your conditions
try this code
foreach (GridViewRow row in yourgridview.Rows)
{
CheckBox myChk = row.FindControl("myControlName") as CheckBox;
if (myChk != null)
{
myChk.Enabled = true;
}
}

How to add Attribute in code behind?

if (e.Item.ItemType == ListViewItemType.DataItem)
{
HtmlTableRow tr = (HtmlTableRow)e.Item.FindControl("itemTemplate"); //tr of table
if (tr != null)
{
foreach (Control c in tr.Controls)
{
HtmlTableCell cell = (HtmlTableCell)c; //td of table.
if (cell != null)
{
cell.Attributes.Add("onmouseover", "this.class='fontBolding'");
}
}
}
}
Here is part of the template of my listview,and my "fontBolding" is definded in a single .css file and I used it in my current page.
But how can I set the attribute.

Get multiple row DataKeys from gridview

Currently I have a Button Click event which takes the DataKey from a row in a Datagrid (via a checkbox) and assigns it to an INT variable to Update an Individual in a databsse. Everything works fine when I only check 1 checkbox and run the Update. However I need the User to have the option to select all checkboxes and take all those Datakeys and assign it to the same variable
INT oNewParentID
Like I said evrything works fine when only 1 checkbox is Selected. I guess I'm asking how to get an Uknoiwn amount of Datakeys or all DataKeys selected by all Checkboxes and store them in the Variable above to run the Update for all Individuals Selected. Here is my Button Click Event so far which is working for 1 checkbox selected:
protected void imgbtnReassgin_Click(object sender, ImageClickEventArgs e)
{
foreach (GridViewRow row in gvSalesmanCustomers.Rows)
{
CheckBox cb = (CheckBox)row.FindControl("chkSalesCustSelector");
if (cb != null && cb.Checked)
{
int oIndividualID = Convert.ToInt32((gvSalesmanCustomers.DataKeys[row.RowIndex].Value));
foreach (GridViewRow r in gvSalesmanByManager.Rows)
{
CheckBox chkBox = (CheckBox)r.FindControl("chkManagerSalesSelector");
if (chkBox != null && chkBox.Checked)
{
int oNewParentID = Convert.ToInt32((gvSalesmanByManager.DataKeys[r.RowIndex].Value));
Individual ind = new Individual();
ind.ReassignIndividual(oIndividualID, oNewParentID);
chkBox.Checked = false;
cb.Checked = false;
}
}
}
}
}
And her is my Update Stored Procedure :
CREATE DEFINER=`root`#`%` PROCEDURE `reassign_Individual`(
IN oIndividualID int(11),
IN oNewParentID int(11)
)
BEGIN
Update intelliair.individual
set ParentID = oNewParentID
Where IndividualID = oIndividualID;END
Re-organized code.
Added break to second foreach assuming that there's no reason to ReasignInvidual multiple times just to assign it to the last selected manager.
protected void imgbtnReassgin_Click(object sender, ImageClickEventArgs e)
{
List<int> ids = new List<int>();
foreach (GridViewRow row in gvSalesmanCustomers.Rows)
{
CheckBox cb = (CheckBox)row.FindControl("chkSalesCustSelector");
if (cb != null && cb.Checked)
{
int oIndividualID = Convert.ToInt32((gvSalesmanCustomers.DataKeys[row.RowIndex].Value));
ids.Add(oIndividualID);
cb.Checked = false;
}
}
int oNewParentID = 0;
foreach (GridViewRow r in gvSalesmanByManager.Rows)
{
CheckBox chkBox = (CheckBox)r.FindControl("chkManagerSalesSelector");
if (chkBox != null && chkBox.Checked)
{
oNewParentID = Convert.ToInt32((gvSalesmanByManager.DataKeys[r.RowIndex].Value));
chkBox.Checked = false;
break; //no reason to go with the same logic to next records
//if you want to uncheck all the checkboxes continue with specific code just for that purpose
}
}
if (ids.Count > 0 && oNewParentID > 0)
foreach(var id in ids)
{
Individual ind = new Individual();
ind.ReassignIndividual(id, oNewParentID);
}
}

iterate through ALL rows in a GridView

<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="chkSelected" runat="server" Checked="false"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>
elow code works fine but there is a bug :
if Employee object has return 5 rows and i am trying to checked the check box based on the ids but instead its just matching only the last id - it suppose to checked all 5 rows..
List<Employee> result = new List<Employee>();
long Id = (long)Session["Id"];
result = Employee.GetEmployeeById(Id);
foreach (GridViewRow row in gv.Rows)
{
CheckBox chkBox = row.FindControl("chkSelected") as CheckBox;
if (c != null)
{
if (result.Count > 0)
{
foreach (Employee item in result)
{
Label Id = row.FindControl("lblId") as Label;
if (Id.Text == item.Id.ToString())
{
chkBox.Checked = true;
}
else
{
chkBox.Checked = false;
}
}
}
Look at your logic - you only have the one checkbox. You're unchecking and checking the same control in the employee loop. Does each grid row have a checkbox that should be selected based on the condition the id exists in the employee list?
foreach (GridViewRow row in gv.Rows)
{
Label Id = row.FindControl("lblId") as Label;
var result = Employee.GetEmployeeById(Id.Text);
if (result.Count > 0)
{
CheckBox chkBox = row.FindControl("chkSelected") as CheckBox;
if (chkBox != null)
{
chkBox.Checked = result.Any(x => x.Id.ToString() == Id.Text);
}
}
}

Adding ListItems to a DropDownList from a generic list

I have a this aspx-code: (sample)
<asp:DropDownList runat="server" ID="ddList1"></asp:DropDownList>
With this codebehind:
List<System.Web.UI.WebControls.ListItem> colors = new List<System.Web.UI.WebControls.ListItem>();
colors.Add(new ListItem("Select Value", "0"));
colors.Add(new ListItem("Red", "1"));
colors.Add(new ListItem("Green", "2"));
colors.Add(new ListItem("Blue", "3"));
ddList1.DataSource = colors;
ddList1.DataBind();
The output looks like this:
<select name="ddList1" id="ddList1">
<option value="Select Value">Select Value</option>
<option value="Red">Red</option>
<option value="Green">Green</option>
<option value="Blue">Blue</option>
</select>
My question is: Why did my values (numbers) disappear and the text used as the value AND the text? I know that it works if I use the ddList1.Items.Add(New ListItem("text", "value")) method, but I need to use a generic list as the datasource for other reasons.
Because DataBind method binds values only if DataValueField property is set. If you set DataValueField property to "Value" before calling DataBind, your values will appear on the markup.
UPDATE: You will also need to set DataTextField property to "Text". It is because data binding and adding items manually do not work in the same way. Databinding does not know the existence of type ListItem and generates markup by evaluating the items in the data source.
And here is the method that performs the data binding. You can exactly see what is going on:
protected internal override void PerformDataBinding(IEnumerable dataSource)
{
base.PerformDataBinding(dataSource);
if (dataSource != null)
{
bool flag = false;
bool flag2 = false;
string dataTextField = this.DataTextField;
string dataValueField = this.DataValueField;
string dataTextFormatString = this.DataTextFormatString;
if (!this.AppendDataBoundItems)
{
this.Items.Clear();
}
ICollection is2 = dataSource as ICollection;
if (is2 != null)
{
this.Items.Capacity = is2.Count + this.Items.Count;
}
if ((dataTextField.Length != 0) || (dataValueField.Length != 0))
{
flag = true;
}
if (dataTextFormatString.Length != 0)
{
flag2 = true;
}
foreach (object obj2 in dataSource)
{
ListItem item = new ListItem();
if (flag)
{
if (dataTextField.Length > 0)
{
item.Text = DataBinder.GetPropertyValue(obj2, dataTextField, dataTextFormatString);
}
if (dataValueField.Length > 0)
{
item.Value = DataBinder.GetPropertyValue(obj2, dataValueField, null);
}
}
else
{
if (flag2)
{
item.Text = string.Format(CultureInfo.CurrentCulture, dataTextFormatString, new object[] { obj2 });
}
else
{
item.Text = obj2.ToString();
}
item.Value = obj2.ToString();
}
this.Items.Add(item);
}
}
if (this.cachedSelectedValue != null)
{
int num = -1;
num = this.Items.FindByValueInternal(this.cachedSelectedValue, true);
if (-1 == num)
{
throw new ArgumentOutOfRangeException("value", SR.GetString("ListControl_SelectionOutOfRange", new object[] { this.ID, "SelectedValue" }));
}
if ((this.cachedSelectedIndex != -1) && (this.cachedSelectedIndex != num))
{
throw new ArgumentException(SR.GetString("Attributes_mutually_exclusive", new object[] { "SelectedIndex", "SelectedValue" }));
}
this.SelectedIndex = num;
this.cachedSelectedValue = null;
this.cachedSelectedIndex = -1;
}
else if (this.cachedSelectedIndex != -1)
{
this.SelectedIndex = this.cachedSelectedIndex;
this.cachedSelectedIndex = -1;
}
}
If you are building ListItems, you have no need to use DataBind() in the first place.
Just add them to your DropDownList:
ddList1.Items.Add(new ListItem("Select Value", "0"));
ddList1.Items.Add(new ListItem("Red", "1"));
ddList1.Items.Add(new ListItem("Green", "2"));
ddList1.Items.Add(new ListItem("Blue", "3"));
DataBind() is useful when you already have a collection/dataobject (usually a DataTable or DataView) that can be used as a DataSource, by setting the DataTextField and DataValueField (as buyutec wrote).
"If you are building ListItems, you have no need to use DataBind() in the first place."
Adding directly to the dropdownlist is the easy way (and given the example code the right one) but lets say you have an unordered datasource and you want the list items sorted.
One way of achieving this would be to create a generic list of ListItem and then use the inherited sort method before databinding to the list.
There are many wys to skin a cat...

Resources