Get Value of hidden column of radgrid telerik in asp.net - 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");

Related

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 fill the text box with value of a column of a GridView row

In the grid I have following columns
<Columns>
<asp:TemplateField HeaderText="select to pay">
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" OnCheckedChanged="chkSelect_OnCheckedChanged" AutoPostBack="true" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Reference" HeaderText="Invoice" HeaderStyle-HorizontalAlign="Center"
ItemStyle-HorizontalAlign="Center">
</asp:BoundField>
<asp:BoundField DataField="ChargedDate" HeaderText="Date of charge" HeaderStyle-HorizontalAlign="Center"
ItemStyle-HorizontalAlign="Center" DataFormatString="{0:MM-dd-yyyy}">
</asp:BoundField>
<asp:BoundField DataField="Amount" HeaderText="Amount" HeaderStyle- HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
</asp:BoundField>
<asp:TemplateField HeaderText="Amount applied">
<ItemTemplate>
<asp:TextBox ID="txtPayAmount" Width="80px" runat="server" AutoPostBack="true" />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
</Columns>
Please tell, In the grid if I chose a check box, the amount value should be displayed in the textbox of the same row. If I checked more than one check boxes the values should displayed in the text boxes of related check boxes/ Rows. the sum should be displayed in the textbox below the grid. What should I need to write under "OnSelect_CheckedChanged" event?
Please try with the below code snippet.
ASPX
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataKeyNames="TotalAmount" ShowFooter="true">
<Columns>
<asp:TemplateField HeaderText="select to pay">
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" OnCheckedChanged="chkSelect_OnCheckedChanged"
AutoPostBack="true" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Amount">
<ItemTemplate>
<%# Eval("TotalAmount", "{0:#,##0.00}")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Amount applied">
<ItemTemplate>
<asp:TextBox ID="txtPayAmount" Width="80px" runat="server" />
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TextBox1" Width="80px" runat="server" />
</FooterTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
</Columns>
</asp:GridView>
ASPX.CS
public partial class Forum : System.Web.UI.Page
{
protected void Page_Init(object sender, System.EventArgs e)
{
GridView gv = new GridView();
gv.ID = "GridView1";
gv.AutoGenerateColumns = false;
gv.ShowFooter = true;
gv.DataKeyNames = new string[] { "TotalAmount" };
TemplateField tf = new TemplateField();
tf.ItemTemplate = new MyCustomTemplate();
gv.Columns.Add(tf);
BoundField bf = new BoundField();
bf.DataField = "TotalAmount";
gv.Columns.Add(bf);
tf = new TemplateField();
tf.ItemTemplate = new MyCustomTextTemplate();
tf.FooterTemplate = new MyCustomFooterTextTemplate();
gv.Columns.Add(tf);
this.form1.Controls.Add(gv);
}
protected void Page_Load(object sender, System.EventArgs e)
{
GridView gv = form1.FindControl("GridView1") as GridView;
if (!IsPostBack)
{
dynamic data = new[] {
new { ID = 1, Name = "Name1",TotalAmount= 10},
new { ID = 2, Name = "Name2",TotalAmount= 20},
new { ID = 3, Name = "Name3",TotalAmount= 30},
new { ID = 4, Name = "Name4",TotalAmount= 40},
new { ID = 5, Name = "Name5",TotalAmount= 50}
};
gv.DataSource = data;
gv.DataBind();
}
}
protected void Page_PreRender(object sender, System.EventArgs e)
{
}
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
dynamic data = new[] {
new { ID = 1, Name ="Name_1"},
new { ID = 2, Name = "Name_2"},
new { ID = 3, Name = "Name_3"},
new { ID = 4, Name = "Name_4"},
new { ID = 5, Name = "Name_5"}
};
}
}
public class MyCustomTemplate : ITemplate
{
public void InstantiateIn(System.Web.UI.Control container)
{
CheckBox cb = new CheckBox();
cb.ID = "chkSelect";
cb.AutoPostBack = true;
cb.CheckedChanged += new EventHandler(cb_CheckedChanged);
container.Controls.Add(cb);
}
protected void cb_CheckedChanged(object sender, EventArgs e)
{
GridViewRow row = (sender as CheckBox).NamingContainer as GridViewRow;
TextBox txtPayAmount = row.FindControl("txtPayAmount") as TextBox;
if ((sender as CheckBox).Checked)
{
txtPayAmount.Text = Convert.ToString((row.Parent.Parent as GridView).DataKeys[row.RowIndex]["TotalAmount"]);
}
else
{
txtPayAmount.Text = string.Empty;
}
int TotalAmount = 0;
foreach (GridViewRow rowv in (row.Parent.Parent as GridView).Rows)
{
CheckBox chk = rowv.FindControl("chkSelect") as CheckBox;
if (chk.Checked)
{
TotalAmount += Convert.ToInt32((row.Parent.Parent as GridView).DataKeys[rowv.RowIndex]["TotalAmount"]);
}
}
GridViewRow rowf = (row.Parent.Parent as GridView).FooterRow;
((TextBox)rowf.FindControl("TextBox1")).Text = TotalAmount.ToString();
}
}
public class MyCustomTextTemplate : ITemplate
{
public void InstantiateIn(System.Web.UI.Control container)
{
TextBox cb = new TextBox();
cb.ID = "txtPayAmount";
container.Controls.Add(cb);
}
}
public class MyCustomFooterTextTemplate : ITemplate
{
public void InstantiateIn(System.Web.UI.Control container)
{
TextBox cb = new TextBox();
cb.ID = "TextBox1";
container.Controls.Add(cb);
}
}
You mean there are rows in the grid with one check box and a text box columns? Each row contain a checkbox and other columns values as mentioned in the code and a Text box? Please attach a image/ screenshot.
I you want to do it using javascript then please try this.
$(function () {
$('input:checkbox').click(function (e) {
calculateSum(5); // sum of 5th column
});
function calculateSum(colidx) {
total = 0.0;
$("tr:has(:checkbox:checked) td:nth-child(" + colidx + ")").each(function () {
var Tot = ($(this).text());
Tot = parseFloat(Tot.replace(/,/g, ''));
total += Tot;
});
$("#<%=txtTotalAmt.ClientID %>").val(total).toFixed(2);
}
});
At OnCheckedChanged event it should be
protected void chkSelect_OnCheckedChanged(object sender, EventArgs e)
{
String czTemp = String.Empty;
GridViewRow gr = (GridViewRow)((CheckBox)sender).Parent.Parent;
GridViewRow footer = grdDues.FooterRow;
TextBox txtAmount = (TextBox)gr.Cells[3].FindControl("txtPayAmount");
TextBox txtAmountPaid = (TextBox)footer.FindControl("txtAmountPaid");
double dbTotalAmt = Convert.ToDouble(txtAmountPaid.Text.Replace("$","").ToString());
if( ((CheckBox)sender).Checked )
{
txtAmount.Enabled = true;
czTemp = grdDues.Rows[gr.RowIndex].Cells[3].Text.ToString();
txtAmount.Text = czTemp.ToString();
dbTotalAmt = dbTotalAmt + Convert.ToDouble(czTemp);
}
else
{
txtAmount.Enabled = false;
czTemp = grdDues.Rows[gr.RowIndex].Cells[3].Text.ToString();
dbTotalAmt = dbTotalAmt - Convert.ToDouble(czTemp);
txtAmount.Text = "0.00";
}
txtAmountPaid.Text = dbTotalAmt.ToString("C");}

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();
}

Multiple delete rows in radgrid

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

One Checkbox to control gridview rows

How can I implement the following: I would like to have a checkbox on the top of my gridview. When checkbox is checked should show all the hidden rows from the gridview but when is unchecked should only show the unhide ones.
I only need 1 checkbox at the top(not in the header of the gridview), I found couple of examples but all of the have checkbox as column and then added to the header as well.
What would be the best way to do this?
Thanks in advance.
This is a one way of doing it on the server:
I define the Gridview and Checkbox as follows:
<asp:CheckBox id="cbShowHidden" runat="server" Text="Show Hidden Rows"
Checked="true" oncheckedchanged="cbShowHidden_CheckedChanged" AutoPostBack="true" ></asp:CheckBox>
<br />
<asp:GridView ID="gvTest" AutoGenerateColumns="false" runat="server"
ShowHeader="true" onrowdatabound="gvTest_RowDataBound">
<Columns>
<asp:BoundField DataField="id" HeaderText="id" />
<asp:BoundField DataField="name" HeaderText="name" />
<asp:BoundField DataField="family" HeaderText="family" />
<asp:BoundField DataField="visibility" HeaderText="visibility" />
</Columns>
</asp:GridView>
This is the codebehind:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
PopulateGrid();
}
}
private void PopulateGrid()
{
gvTest.DataSource = Enumerable.Range(0, 3000).Select(i => new { id = i, name = 2 * i, family = "Unknown", visibility = i % 3 == 0 ? "Visible" : "Hidden" });
gvTest.DataBind();
}
protected void gvTest_RowDataBound(object sender, GridViewRowEventArgs e)
{
bool showHidden = cbShowHidden.Checked;
if (e.Row.RowType == DataControlRowType.DataRow)
e.Row.Visible = showHidden || string.Compare(e.Row.Cells[3].Text, "Visible") == 0;
}
protected void cbShowHidden_CheckedChanged(object sender, EventArgs e)
{
PopulateGrid();
}
This can be greatly optimized by doing the hiding on the client with jQuery.

Resources