Multiple delete rows in radgrid - asp.net

My problem :
I want to delete all selected records using checkbox in RadGrid..
How to write the code for this..
I have a code for simple GridView But its not working in RadGrid.

Please try with below code snippet.
Method1
.aspx
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False"
OnNeedDataSource="RadGrid1_NeedDataSource">
<MasterTableView DataKeyNames="ID">
<Columns>
<telerik:GridBoundColumn DataField="Name" UniqueName="Name" HeaderText="Name">
</telerik:GridBoundColumn>
<telerik:GridTemplateColumn>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridEditCommandColumn>
</telerik:GridEditCommandColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
.aspx.cs
protected void Button1_Click(object sender, EventArgs e)
{
foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
{
CheckBox CheckBox1 = item.FindControl("CheckBox1") as CheckBox;
if (CheckBox1 != null && CheckBox1.Checked)
{
// Access data key
string strID = item.GetDataKeyValue("ID").ToString();
// Access column
string strName = item["Name"].Text; // "Name" is column unique name
// delete logic comes here
}
}
}
Method2
.aspx
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False"
OnNeedDataSource="RadGrid1_NeedDataSource"
AllowMultiRowSelection="true">
<MasterTableView DataKeyNames="ID">
<Columns>
<telerik:GridClientSelectColumn>
</telerik:GridClientSelectColumn>
<telerik:GridBoundColumn DataField="Name" UniqueName="Name" HeaderText="Name">
</telerik:GridBoundColumn>
<telerik:GridEditCommandColumn>
</telerik:GridEditCommandColumn>
</Columns>
</MasterTableView>
<ClientSettings>
<Selecting AllowRowSelect="true" />
</ClientSettings>
</telerik:RadGrid>
.aspx.cs
protected void Button1_Click(object sender, EventArgs e)
{
foreach (GridDataItem item in RadGrid1.SelectedItems)
{
if (item.Selected)
{
// Access data key
string strID = item.GetDataKeyValue("ID").ToString();
// Access column
string strName = item["Name"].Text; // "Name" is column unique name
// delete logic comes here
}
}
}
Common code for both of above method.
.aspx
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
.aspx.cs
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
dynamic data = new[] {
new { ID = 1, Name ="Name1",path="1.jpg"},
new { ID = 2, Name = "Name2",path="2.jpg"},
new { ID = 3, Name = "Name3",path="3.jpg"},
new { ID = 4, Name = "Name4",path="2.jpg"},
new { ID = 5, Name = "Name5",path="3.jpg"}
};
RadGrid1.DataSource = data;
}

Related

get value from asp boundfield and store to sql database via button click?

i want to get this value from webform 1 (admin side) when button is clicked:
here is my full viewcreditrequest code:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
CssClass="table table-hover table-striped"
onselectedindexchanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:BoundField DataField="Username" HeaderText="Username"
SortExpression="Username" />
<asp:BoundField DataField="LastName" HeaderText="LastName"
SortExpression="LastName" />
<asp:BoundField DataField="FirstName" HeaderText="FirstName"
SortExpression="FirstName" />
<asp:BoundField DataField="CompanyName" HeaderText="CompanyName"
SortExpression="CompanyName" />
<asp:BoundField DataField="EmailAddress" HeaderText="EmailAddress"
SortExpression="EmailAddress" />
<asp:BoundField DataField="CompanyAddress" HeaderText="CompanyAddress"
SortExpression="CompanyAddress" />
<asp:BoundField DataField="IncomeRange" HeaderText="IncomeRange"
SortExpression="IncomeRange" />
<asp:BoundField DataField="CreditRequest" HeaderText="CreditRequest"
SortExpression="CreditRequest" />
<asp:BoundField DataField="ContactNumber" HeaderText="ContactNumber" SortExpression="ContactNumber" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Button1" runat="server" Text="Approve" CommandName="Approve" CommandArgument='<%# Eval("CreditRequest") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
code behind:
protected void Page_Load(object sender, EventArgs e)
{
if (Session["IslandGasAdminFM"] != null)
{
bindgrid();
Label1.Text = "- Finance Manager";
}
else
{
Response.Write("<script>alert('Finance Manager credentials needed'); window.location.href='LogIn.aspx';</script>");
}
}
public void bindgrid()
{
SqlConnection conn = new SqlConnection("Data Source = 'PAULO'; Initial Catalog=ShoppingCartDB;Integrated Security =True");
SqlCommand cmd = new SqlCommand("select * from CreditRequests ", conn);
SqlDataAdapter da = new SqlDataAdapter("", conn);
da.SelectCommand = new SqlCommand("select * from CreditRequests", conn);
DataSet ds = new DataSet();
da.Fill(ds, "data");
GridView1.DataSource = ds.Tables[0].DefaultView;
GridView1.DataBind();
}
and pass it on to webform 2 (client side):
<asp:TextBox ID="txtCredit" runat="server"></asp:TextBox>
this is what i got so far:
admin side:
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnApprove" runat="server" Text="Approve" OnClick ="btnApprove_Click" />
</ItemTemplate>
</asp:TemplateField>
code behind:
protected void btnApprove_Click(object sender, EventArgs e)
{
//code for getting the boundfield "creditrequest" and if possible, store it in database for future use.
}
is there any way to get the creditrequest value from boundfield and store it in database?
Here you can do two thing's 1.since you are using Button, add a new row in gridview like this
<asp:TemplateField HeaderText="Actions">
<ItemTemplate>
<asp:Button CommandArgument='<%#Eval("CreditRequest") %>' runat="server" Text="View" ID="btnEdit" OnClick="btnEdit_OnClick"/>
</ItemTemplate>
</asp:TemplateField>
and the code behind is like this
protected void btnEdit_OnClick(object sender, EventArgs e)
{
if (sender != null)
{
string id = ((Button)sender).CommandArgument.ToString();
Response.Redirect("yourpagename.aspx?value=" + id);
}
}
and on another page Client side get the query string value like this
string value = Request.QueryString["value"])
And 2nd and easy method will be to use hyperlink instead of button like this
<asp:TemplateField HeaderText="Actions">
<ItemTemplate>
<asp:HyperLink runat="server" NavigateUrl='<%# Eval("CreditRequest", "~/yourpagename.aspx?value={0}") %>' Text="Edit" "></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
Copy the code below EXACTLY as I'm showing and it will work. I've just tested on my computer:
.ASPX:
<asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="CreditRequest" HeaderText="Credit Request" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button runat="server" Text="Approve" CommandName="Approve" CommandArgument='<%# Eval("CreditRequest") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code behind:
public partial class delete_me : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)//THIS IS IMPORTANT.GridView1_RowCommand will not fire unless you add this line
{
var p1 = new Person() { Name = "Person 1",CreditRequest="Credit Request 1" };
var p2 = new Person() { Name = "Person 2",CreditRequest="Credit Request 2" };
var list = new List<Person> { p1, p2 };
GridView1.DataSource = list;
GridView1.DataBind();
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
System.Diagnostics.Debugger.Break();
if(e.CommandName == "Approve")
{
string creditRequest = e.CommandArgument as string;
}
}
}
public class Person
{
public string Name { get; set; }
public string CreditRequest { get; set; }
}

How can I make RadGrid expand only one detail table based on the command clicked?

I have a master view with two child views. I would like to have command buttons on the master row which cause an expand to happen to one or the other child tables. When I handle the ItemCommand event, I hide set all detail tables to Visible=False but they are still visible on the output.
I've tried setting the expanded and visibility of the detail tables right within the ItemCommand event like this:
if (e.CommandName == "ExpandSomething")
{
item.Expanded = true;
foreach (var table in MyGrid.MasterTableView.DetailTables)
if (table.Name != "Something")
table.Visible = false;
}
This had no effect on the Grid.
Then I thought maybe the hiding needed to be done later in the event cycle so I tried it like this:
private string _activeChildView = null;
protected void ShowSingleChildTable(string name) //this is called from item command
{
_activeChildView = name;
}
private void Page_PreRender(object sender, EventArgs e)
{
if (_activeChildView != null)
foreach (var table in AdminGrid.MasterTableView.DetailTables)
if (table.Name != _activeChildView)
table.Visible = false;
}
Neither way works - both grids are always visible.
In playing around with different events, I've found that the DataBinding event is the only place that setting the visibility to false works, but unfortunately that fires before the ItemCommand so I can't figure out if a detail button was pressed.
Any other ideas for a conditional detail view like the one described?
EDIT, here's the markup, if you think it'll help. I've removed the unneeded column definitions:
<asp:EntityDataSource ID="EntityDataSourceThinges" runat="server"
ConnectionString="name=MyProjectEntities"
EntitySetName="Thinges" DefaultContainerName="MyProjectEntities"
EnableDelete="true" EnableUpdate="true" EnableInsert="true"
Include="ThingSomethings"
OrderBy="it.Title"
Where="it.Title like '%' + #Title + '%' or #Title is null">
<WhereParameters>
<asp:ControlParameter ControlID="txtSearch" DefaultValue="%" Name="Title" PropertyName="Text" Type="String" />
</WhereParameters>
</asp:EntityDataSource>
<asp:EntityDataSource ID="EntityDataSourceThingSomethings" runat="server"
ConnectionString="name=MyProjectEntities"
EntitySetName="ThingSomethings" DefaultContainerName="MyProjectEntities"
EnableDelete="true" EnableUpdate="true"
AutoGenerateWhereClause="true">
<WhereParameters>
<asp:SessionParameter Name="ThingID" SessionField="ThingID" Type="Int32" />
</WhereParameters>
</asp:EntityDataSource>
<asp:EntityDataSource ID="EntityDataSourceImages" runat="server"
ConnectionString="name=MyProjectEntities"
EntitySetName="MyProjectImages" DefaultContainerName="MyProjectEntities"
EnableDelete="true" EnableUpdate="true" EnableInsert="true"
AutoGenerateWhereClause="true"
OrderBy="it.Sequence">
<WhereParameters>
<asp:SessionParameter Name="ThingID" SessionField="ThingID" Type="Int32" />
</WhereParameters>
</asp:EntityDataSource>
Search by title:
<asp:TextBox runat="server" ID="txtSearch" /><asp:Button runat="server" Text="Search" />
<telerik:RadGrid ID="DnnGrid1" runat="server"
DataSourceID="EntityDataSourceThinges"
ImagesPath="~/images"
AllowPaging="false" AllowSorting="true"
AllowAutomaticUpdates="true" AllowAutomaticInserts="true" AllowAutomaticDeletes="true">
<MasterTableView AutoGenerateColumns="false" CommandItemDisplay="TopAndBottom"
HierarchyLoadMode="ServerOnDemand" ExpandCollapseColumn-Visible="false"
DataKeyNames="ThingID">
<Columns>
...
<telerik:GridButtonColumn Text="Expand Something" CommandName="ExpandSomething" />
<telerik:GridButtonColumn Text="Expand Something Else" CommandName="ExpandSomethingElse" />
...
<telerik:GridButtonColumn ButtonType="ImageButton" CommandName="Delete" ConfirmText="Are you sure you want to permenently delete this record?" />
</Columns>
<DetailTables>
<telerik:GridTableView runat="server" Name="Images" DataKeyNames="MyProjectImageID" DataSourceID="EntityDataSourceImages"
AutoGenerateColumns="false"
AllowAutomaticUpdates="true" AllowAutomaticInserts="true" AllowAutomaticDeletes="true"
CommandItemDisplay="TopAndBottom">
<ParentTableRelation>
<telerik:GridRelationFields MasterKeyField="ThingID" DetailKeyField="ThingID" />
</ParentTableRelation>
<Columns>
...
</Columns>
</telerik:GridTableView>
<telerik:GridTableView runat="server" Name="Something" DataSourceID="EntityDataSourceThingSomethings"
AutoGenerateColumns="false"
AllowAutomaticUpdates="true" AllowAutomaticDeletes="true"
CommandItemDisplay="TopAndBottom" DataKeyNames="SomethingID,ThingID">
<ParentTableRelation>
<telerik:GridRelationFields MasterKeyField="ThingID" DetailKeyField="ThingID" />
</ParentTableRelation>
<Columns>
...
</Columns>
<EditFormSettings EditFormType="AutoGenerated" />
<CommandItemSettings ShowAddNewRecordButton="false" />
</telerik:GridTableView>
</DetailTables>
<EditFormSettings EditFormType="AutoGenerated" />
</MasterTableView>
</telerik:RadGrid>
Please try with the below code snippet.
ASPX
<telerik:RadGrid ID="RadGrid1" runat="server" OnNeedDataSource="RadGrid1_NeedDataSource" OnDetailTableDataBind="RadGrid1_DetailTableDataBind"
OnItemCommand="RadGrid1_ItemCommand" AutoGenerateColumns="false">
<MasterTableView DataKeyNames="ID">
<Columns>
<telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID">
</telerik:GridBoundColumn>
<telerik:GridButtonColumn Text="Expand Something" CommandName="ExpandSomething" />
<telerik:GridButtonColumn Text="Expand Something Else" CommandName="ExpandSomethingElse" />
</Columns>
<DetailTables>
<telerik:GridTableView runat="server" Name="Images" AutoGenerateColumns="false" CommandItemDisplay="TopAndBottom">
<Columns>
<telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID">
</telerik:GridBoundColumn>
</Columns>
</telerik:GridTableView>
<telerik:GridTableView runat="server" Name="Something" AutoGenerateColumns="false" CommandItemDisplay="TopAndBottom">
<Columns>
<telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID">
</telerik:GridBoundColumn>
</Columns>
</telerik:GridTableView>
</DetailTables>
</MasterTableView>
</telerik:RadGrid>
Method 1: Show only 1 child grid in each row.
ASPX.CS
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
dynamic data = new[] {
new { ID = 1, Name ="Name1"},
new { ID = 2, Name ="Name2"} };
RadGrid1.DataSource = data;
}
protected void RadGrid1_DetailTableDataBind(object sender, GridDetailTableDataBindEventArgs e)
{
GridDataItem parentItem = e.DetailTableView.ParentItem as GridDataItem;
if (e.DetailTableView.Name == "Images")
{
dynamic data = new[] {
new { ID = 11, Name ="Name11"},
new { ID = 12, Name ="Name12"} };
e.DetailTableView.DataSource = data;
}
else if (e.DetailTableView.Name == "Something")
{
dynamic data = new[] {
new { ID = 111, Name ="Name111"},
new { ID = 112, Name ="Name112"} };
e.DetailTableView.DataSource = data;
}
}
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
if (e.CommandName == "ExpandSomething")
{
GridDataItem item = e.Item as GridDataItem;
item.Expanded = true;
if (e.Item.HasChildItems)
{
item.ChildItem.NestedTableViews[0].Visible = false;
item.ChildItem.NestedTableViews[1].Visible = true;
}
}
else if (e.CommandName == "ExpandSomethingElse")
{
GridDataItem item = e.Item as GridDataItem;
item.Expanded = true;
if (e.Item.HasChildItems)
{
item.ChildItem.NestedTableViews[0].Visible = true;
item.ChildItem.NestedTableViews[1].Visible = false;
}
}
}
Method 2: Show only 1 child grid in full grid.
ASPX.CS
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
dynamic data = new[] {
new { ID = 1, Name ="Name1"},
new { ID = 2, Name ="Name2"} };
RadGrid1.DataSource = data;
}
protected void RadGrid1_DetailTableDataBind(object sender, GridDetailTableDataBindEventArgs e)
{
GridDataItem parentItem = e.DetailTableView.ParentItem as GridDataItem;
if (e.DetailTableView.Name == "Images")
{
dynamic data = new[] {
new { ID = 11, Name ="Name11"},
new { ID = 12, Name ="Name12"} };
e.DetailTableView.DataSource = data;
}
else if (e.DetailTableView.Name == "Something")
{
dynamic data = new[] {
new { ID = 111, Name ="Name111"},
new { ID = 112, Name ="Name112"} };
e.DetailTableView.DataSource = data;
}
}
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
if (e.CommandName == "ExpandSomething")
{
GridDataItem item = e.Item as GridDataItem;
item.Expanded = true;
if (e.Item.HasChildItems)
{
item.ChildItem.NestedTableViews[0].Visible = false;
item.ChildItem.NestedTableViews[1].Visible = true;
}
foreach (GridDataItem otheritem in RadGrid1.MasterTableView.Items)
{
if (otheritem.Expanded && item.GetDataKeyValue("ID").ToString() != otheritem.GetDataKeyValue("ID").ToString())
{
otheritem.Expanded = false;
}
}
}
else if (e.CommandName == "ExpandSomethingElse")
{
GridDataItem item = e.Item as GridDataItem;
item.Expanded = true;
if (e.Item.HasChildItems)
{
item.ChildItem.NestedTableViews[0].Visible = true;
item.ChildItem.NestedTableViews[1].Visible = false;
}
foreach (GridDataItem otheritem in RadGrid1.MasterTableView.Items)
{
if (otheritem.Expanded && item.GetDataKeyValue("ID").ToString() != otheritem.GetDataKeyValue("ID").ToString())
{
otheritem.Expanded = false;
}
}
}
}
Let me know if any concern.

How to show Membership Roles in the Gridview as Columns Dynamically

I am Using Membership to define roles in Asp.net.Now as per my requirement i need to show the Roles as dynamic Columns of gridview with first two columns as fixed and rest of the columns as per the number of Roles in table but i have no idea of how to meet that as i have never worked on it before..
Here is my static gridview code in aspx page...
<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound"
AutoGenerateColumns="False" BackColor="LightGoldenrodYellow" BorderColor="Tan"
BorderWidth="1px" CellPadding="2" ForeColor="Black" GridLines="None"
Width="477px">
<AlternatingRowStyle BackColor="PaleGoldenrod" />
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="chkhdr" runat="server" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkChild" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Username">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("col0") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Role(Admin)">
<ItemTemplate>
<asp:CheckBox ID="chkAdmin" runat="server" Checked='<%# Eval("col1") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Role(DPAO User )">
<ItemTemplate>
<asp:CheckBox ID="chkUser" runat="server" Checked='<%# Eval("col2") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Role(GeneralUser)">
<ItemTemplate>
<asp:CheckBox ID="chkgen" runat="server" Checked='<%# Eval("col3") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And here is the code to load grid with data...
protected void BindGridviewData()
{
var role = from MembershipUser u in Membership.GetAllUsers()
select new
{
User = u.UserName,
Role = string.Join(",", Roles.GetRolesForUser(u.UserName))
};
DataTable dTable = new DataTable();
dTable.Columns.Add("col0", typeof(string));
dTable.Columns.Add("col1", typeof(bool));
dTable.Columns.Add("col2", typeof(bool));
dTable.Columns.Add("col3", typeof(bool));
foreach (MembershipUser u in Membership.GetAllUsers())
{
DataRow dRow = dTable.NewRow();
dRow[0] = u.UserName;
string[] roles = Roles.GetRolesForUser(u.UserName);
dRow[1] = roles.Contains("Admin") ? true : false;
dRow[2] = roles.Contains("DPAO User") ? true : false;
dRow[3] = roles.Contains("GeneralUser") ? true : false;
dTable.Rows.Add(dRow);
}
GridView1.DataSource = dTable;
GridView1.DataBind();
}
In the given Grid I need first two columns as fixed and other column based on value from the Role table from membership...
Any help will be highly appreciated.Thanks in advance..
For dynamically Show , Update roles and Delete records try this:
ASPX:
<asp:GridView ID="GridView1" runat="server" onrowdatabound="GridView1_RowDataBound1"/></asp:GridView>
<asp:Button ID="cmdDelete" runat="server" onclick="cmdDelete_Click1" Text="Delete" />
<asp:Button ID="cmdUpdateRole" runat="server" onclick="cmdUpdateRole_Click" Text="Update Roles" />
Code behind:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridviewData();
}
}
protected void BindGridviewData()
{
DataTable dTable = new DataTable();
dTable.Columns.Add("Select", typeof(bool));
dTable.Columns.Add("Username", typeof(string));
dTable.Columns.Add("Role(Admin)", typeof(bool));
dTable.Columns.Add("Role(DPAO User)", typeof(bool));
dTable.Columns.Add("Role(GeneralUser)", typeof(bool));
foreach (MembershipUser u in Membership.GetAllUsers())
{
DataRow dRow = dTable.NewRow();
dRow[0] = false;
dRow[1] = u.UserName;
string[] roles = Roles.GetRolesForUser(u.UserName);
dRow[2] = roles.Contains("Admin") ? true : false;
dRow[3] = roles.Contains("DPAO User") ? true : false;
dRow[4] = roles.Contains("GeneralUser") ? true : false;
dTable.Rows.Add(dRow);
}
GridView1.DataSource = dTable;
GridView1.DataBind();
}
protected void cmdUpdateRole_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
string username = row.Cells[1].Text;
CheckBox chkAdmin = (CheckBox)row.Cells[2].Controls[0];
CheckBox chkUser = (CheckBox)row.Cells[3].Controls[0];
CheckBox chkgen = (CheckBox)row.Cells[4].Controls[0];
List<string> roles=new List<string>();
if (chkAdmin.Checked)
roles.Add("Admin");
if (chkUser.Checked)
roles.Add("DPAO User");
if (chkgen.Checked)
roles.Add("GeneralUser");
if (Roles.GetRolesForUser(username).Length > 0)
{
Roles.RemoveUserFromRoles(username, Roles.GetRolesForUser(username));
}
if (roles.Count > 0)
{
Roles.AddUserToRoles(username, roles.ToArray());
}
BindGridviewData();
}
}
protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox c0 = (CheckBox)e.Row.Cells[0].Controls[0];
CheckBox c2 = (CheckBox)e.Row.Cells[2].Controls[0];
CheckBox c3 = (CheckBox)e.Row.Cells[3].Controls[0];
CheckBox c4 = (CheckBox)e.Row.Cells[4].Controls[0];
c0.Enabled=c2.Enabled =c3.Enabled=c4.Enabled= true;
}
}
protected void cmdDelete_Click1(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox chk = (CheckBox)row.Cells[0].Controls[0];
if (chk.Checked)
{
string username = row.Cells[1].Text;
Membership.DeleteUser(username);
BindGridviewData();
}
}
}

Get Value of hidden column of radgrid telerik in asp.net

I have a radgrid in which I hide Id column. Now I want to get its value on linkbutton click. If column is visible it work fine but
it show blank value when it is invisible. my code is
protected void RadGrid1_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
{
if (e.CommandName == "Detail")
{
GridDataItem dataItm = e.Item as GridDataItem;
string value = dataItm["Id"].Text;
}
}
Please try with the below code snippet.
ASPX
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnNeedDataSource="RadGrid1_NeedDataSource"
OnItemCommand="RadGrid1_ItemCommand">
<MasterTableView DataKeyNames="ID">
<Columns>
<telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="ID" UniqueName="ID1" HeaderText="ID" Visible="false">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="ID" UniqueName="ID2" HeaderText="ID" Display="false">
</telerik:GridBoundColumn>
<telerik:GridTemplateColumn>
<ItemTemplate>
<asp:Button ID="Button1" runat="server" CommandName="Detail" CommandArgument='<%# Eval("ID") %>' />
</ItemTemplate>
</telerik:GridTemplateColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
ASPX.CS
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
dynamic data = new[] {
new { ID = 1, Name ="Name1"},
new { ID = 2, Name = "Name2"},
new { ID = 3, Name = "Name3"},
new { ID = 4, Name = "Name4"},
new { ID = 5, Name = "Name5"}
};
RadGrid1.DataSource = data;
}
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
if (e.CommandName == "Detail")
{
GridDataItem item = e.Item as GridDataItem;
string strID = item.GetDataKeyValue("ID").ToString(); // We are able to get ID field value here
string strID1 = item["ID1"].Text; // We are NOT able to get ID field value here Because column is Visible false
string strID2 = item["ID2"].Text; // We are able to get ID field value here
string strCommandArgument = e.CommandArgument.ToString(); // We are able to get ID field value here
}
}
Please use Display property in place-of Visible property.
The easiest way is to set Visible = true and Display=false and you should be fine.
Is the column always invisible? If so, you can put the ID in the DataKeyNames property as in:
DataKeyNames="ID"
And then access it via:
var id = (int)dataItm.getDataKeyValue("ID");

Changing Telerik RadGrid PageSize based on other RadGrid PageSize

I got a two RadGrid controls in my page RadGrid1 and RadGrid2 , When I change page size of any one of them I want the other one changed automatically with the same page size , I tried this :
protected void RadGrid1_PageSizeChanged(object sender, GridPageSizeChangedEventArgs e)
{
RadGrid2.PageSize = e.NewPageSize;
}
protected void RadGrid2_PageSizeChanged(object sender, GridPageSizeChangedEventArgs e)
{
RadGrid1.PageSize = e.NewPageSize;
}
but this causes a stackoverflow exception because each event called the other.
Please try with below code snippet.
.aspx
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False" OnItemDataBound="RadGrid1_ItemDataBound"
OnNeedDataSource="RadGrid1_NeedDataSource" AllowFilteringByColumn="true"
PageSize="2" AllowPaging="true" onpagesizechanged="RadGrid1_PageSizeChanged">
<MasterTableView Name="Parent" DataKeyNames="ID" EditMode="PopUp">
<Columns>
<telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="Name" UniqueName="Name" HeaderText="Name">
</telerik:GridBoundColumn>
<telerik:GridEditCommandColumn>
</telerik:GridEditCommandColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
<telerik:RadGrid ID="RadGrid2" runat="server" AutoGenerateColumns="False" OnNeedDataSource="RadGrid2_NeedDataSource" onpagesizechanged="RadGrid2_PageSizeChanged"
PageSize="2" AllowPaging="true">
<MasterTableView Name="Parent" DataKeyNames="ID" EditMode="PopUp">
<Columns>
<telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="Name" UniqueName="Name" HeaderText="Name">
</telerik:GridBoundColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
.aspx.cs
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
dynamic data = new[] {
new { ID = 1, Name ="Name1",path="1.jpg"},
new { ID = 2, Name = "Name2",path="2.jpg"},
new { ID = 3, Name = "Name3",path="3.jpg"},
new { ID = 4, Name = "Name4",path="2.jpg"},
new { ID = 5, Name = "Name5",path="3.jpg"}
};
RadGrid1.DataSource = data;
}
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item.IsInEditMode && e.Item is GridEditableItem)
{
GridEditableItem item = e.Item as GridEditableItem;
string strID = item.GetDataKeyValue("ID").ToString();
// Access your DataKey here
}
}
protected void RadGrid2_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
dynamic data = new[] {
new { ID = 1, Name ="Name1",path="1.jpg"},
new { ID = 2, Name = "Name2",path="2.jpg"},
new { ID = 3, Name = "Name3",path="3.jpg"},
new { ID = 4, Name = "Name4",path="2.jpg"},
new { ID = 5, Name = "Name5",path="3.jpg"}
};
RadGrid2.DataSource = data;
}
protected void RadGrid1_PageSizeChanged(object sender, GridPageSizeChangedEventArgs e)
{
RadGrid2.PageSize = e.NewPageSize;
RadGrid2.Rebind();
}
protected void RadGrid2_PageSizeChanged(object sender, GridPageSizeChangedEventArgs e)
{
RadGrid1.PageSize = e.NewPageSize;
RadGrid1.Rebind();
}

Resources