I am using a telerik RadGrid that is being populated via a ObjectDataSouce. This object returns a series of boolean fields
<telerik:GridCheckBoxColumn DataField="IsSysAdmin" DataType="System.Boolean"FilterControlAltText="Filter IsSysAdmin column" HeaderText="Sys Admin"
SortExpression="IsSysAdmin" UniqueName="IsSysAdmin">
Once I select a column I would like to be able to derive the boolean value for use in another section of the page.
I can get at the values in the selected rows by doing the following:
protected void gv_roleList_Command(object sender, GridCommandEventArgs e)
{
if (e.CommandName == "EditItem")
{
GridDataItem item = (GridDataItem)e.Item;
item.Selected = true;
txt_RoleName.Text = item["RoleName"].Text;
...edited for brevity
By calling the column I can get the values however this does not work for a GridCheckBoxColumn the text attribute only returns (which I would expect).
I have tired to cast the sender as checkbox to go at it that way but my implementation does not seem to work.
var cb = (GridCheckBoxColumn)sender;
Does anyone have any tips on how to go about deriving the value from the Checkbox column?
Cheers
Please check below code snippet.
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
if (e.CommandName == "EditItem")
{
// if current row is in normal mode
GridDataItem item = e.Item as GridDataItem;
CheckBox chk = item["IsSysAdmin"].Controls[0] as CheckBox;
// If your row is in edit mode
GridEditableItem eitem = e.Item as GridEditableItem;
CheckBox echk = eitem["IsSysAdmin"].Controls[0] as CheckBox;
}
}
Related
Its a weird requirement but, Can I alter values of a single column of a databound gridview and display it in the same gridview ? Say, in GridView_RowDataBound(object sender, GridViewRowEventArgs e). I have edited the code for what I am actually doing. Problem is "DataItem" is a typeof class(entity fetched db table) and converting it to Datarow is not possible. So how do i go about it?
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataRowView drv = (DataRowView)e.Row.DataItem;
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (drv["Id"] != DBNull.Value)
{
string val = CommonUtil.Decrypt(drv["Id"].ToString());
e.Row.Cells[3].Text = val ;
}
}
}
Yes it's doable in GridView RowDataBound something like:
if(e.Row.RowType==DataControlRowType.DataRow)
{
//say you want to set value of 3rd column to "Hello"
// e.Row.Cells[2].Text="Hello"; //0 based index
MyCustomClass myitem= (MyCustomClass) e.Row.DataItem;
if (myitem.Id != null)
{
string val = CommonUtil.Decrypt(myitem.Id.ToString());
e.Row.Cells[3].Text = val ;
}
}
If that doesn't help, please share some code and more information on exactly what column you want to change and to what value.
You can cast to the type you want:
MyCustomClass drv = (MyCustomClass) e.Row.DataItem;
I have a button (item template) in any rows of a gridView.
I want when user clicks on this button, I retrieve dataKey of row that there is that button in it.
how can I do this?
(sorry,I can't speak and write English very well)
Assuming you have one DataKey and it is an int and the row of the button isn't necessarily selected and you have connected the following function to the buttons in your template field:
protected void RowClicked(object sender, EventArgs e)
{
Button TempButton = sender as Button;
if (TempButton != null)
{
GridViewRow GVR = TempButton.Parent.Parent as GridViewRow;
if (GVR != null)
{
int ID = (int) GridView1.DataKeys[GVR.DataItemIndex].Value;
}
}
}
Try the SelectedIndexChanged event. Something like this:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e){
TextBox1.Text = GridView1.SelectedRow.Cells[1].Text;
TextBox2.Text = GridView1.SelectedRow.Cells[2].Text;
.
.
.
}
I am trying to change fields to a Checkbox inside a GridView.
I currently create Grid Columns Dynamically based on a query and some of the columns I want to change to a checkbox so it can be checked/unchecked by the user. I know I cant do this via .aspx page using but I am trying to stay away from statically creating the fields.
Any help would be great.
Make use of GridView's RowDataBound Event. Thus you can add any control to your GridView.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox chk1 = new CheckBox();
chk1.ID = "chkbox1";
e.Row.Cells[0].Controls.Add(chk1);
}
}
Edit for Comment:
Once you passed the values from database to gridview (out of scope for this question), You can access the values using e.Row.Cells[i].Text where "i" is the row.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
TextBox txt1 = new TextBox();
txt1.Text = e.Row.Cells[0].Text;
e.Row.Cells[0].Controls.Add(txt1);
}
}
When handling the OnUpdateCommand event on a RadGrid the DataItem is null.
I thought that this would also represent the data item being represented by the row.
The Radgrid is populated from an IList and in the handler the code looks like this...
protected void rgAllocatedClients_UpdateCommand(object sender, GridCommandEventArgs e)
{
if (e.Item is GridDataItem)
{
var gridDataItem = e.Item as GridDataItem;
var client= gridDataItem .DataItem as Client;
....
....
This works find when handling the ItemDataBound event but not when handling the UpdateCommand event. I really need this as in my Client class is the Id of the row I want to handle the update for.
Thanks,
Assuming your Grid is in Edit mode befor ethe Update Command, you should cast e.Item to GridEditableItem instead of GridDataItem
Try this by using GridEditableItem
protected void grdContacts_UpdateCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
{
string idEditing = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Id"].ToString();
GridEditableItem editedItem = e.Item as GridEditableItem;
Hashtable newValues = new Hashtable();
// ur code
}
So far I have done this, I am not sure whether this is right or wrong
public partial class _Default : System.Web.UI.Page
{
Label l = new Label();
GridView gv = new GridView();
protected void Page_Load(object sender, EventArgs e)
{
for (int i = 0; i < 5; i++)
{
GridViewRow gvr = new GridViewRow(i, i, DataControlRowType.DataRow, DataControlRowState.Normal);
gvr.Controls.Add(l);
gv. (what to do here)
}
this.Controls.Add(gv);
}
}
please help
gv.Rows.Add(gvr);
If you're starting with an empty GridView, an easier way to dynamically create x rows is to create a dummy list and then set it to the data source:
var list = new List<string>(10); // replace 10 with number of empty rows you want
// for loop to add X items to the list
gv.DataSource = list;
gv.DataBind();
If you are doing this, I'd recommend doing it with a Repeater. It's a lot easier to manage.
The DataGrid fires the RowCreate event when a new row is created.
Collapse
//OnRowCreated="GridView3_RowCreated"
protected void GridView3_RowCreated(object sender, GridViewRowEventArgs e)
{
//When a child checkbox is unchecked then the header checkbox will also be unchecked.
if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState ==
DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate))
{
CheckBox chkBxSelect = (CheckBox)e.Row.Cells[1].FindControl("chkselect");
CheckBox chkBxHeader = (CheckBox)this.GridView3.HeaderRow.FindControl("chkHeader");
chkBxSelect.Attributes["onclick"] = string.Format("javascript:ChildClick(
this,'{0}');", chkBxHeader.ClientID);
}
}