How to get radgrid (Telerik) row count in asp.net - telerik

How to get radgrid (Telerik) row count in asp.net and how to get cell value from Telerik to assign a variable

Here you go:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridPagerItem)
{
GridPagerItem pagerItem = e.Item as GridPagerItem;
int itemsCount = pagerItem.Paging.DataSourceCount;
Label1.Text = "Total items count: " + itemsCount;
}
}
For more info check out this thread: https://www.telerik.com/forums/how-to-get-total-rows-count-of-radgrid
This article will show you how to access the cells on the server: https://docs.telerik.com/devtools/aspnet-ajax/controls/grid/accessing-values-and-controls/server-side/accessing-cells

Related

Telerik Radgrid Access Values

I want to access values of the insert form in Telerik radgrid. how can i do that this is what i am trying
protected void RadGrid3_InsertCommand(object sender, GridCommandEventArgs e)
{
if (e.CommandName == RadGrid.PerformInsertCommandName)
{
GridEditableItem editedItem = e.Item as GridEditableItem;
string a = (editedItem.FindControl("ID") as TextBox).Text;
string b = (editedItem.FindControl("Quantity") as TextBox).Text;
}
}
It throws me the following error
Object reference not set to an instance of an object.
When editing or inserting a grid item, you could access and modify the controls generated in the editable item (reference Telerik documentation).
Try something like this:
protected void RadGrid3_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridEditableItem && e.Item.IsInEditMode)
{
GridEditableItem editableItem = e.Item as GridEditableItem;
string a= editableItem["Id"].Controls[0] as TextBox;
string b= editableItem["Quantity"].Controls[0] as TextBox;
}
}
Please note that I have not tested the the above snippet. For more references you can look here

How to delete the listbox item in wp7?

Listbox having 2 buttons.When click on button need to delete the item from that listbox.
please tell me how to acheive that?
List<SampleCheckedData> interestrates = new List<SampleCheckedData>();
interestrates = (from rts in xmlDocu.Descendants("Friend")
select new SampleCheckedData
{
Id = (string)rts.Element("userid"),
Name = (string)rts.Element("name"),
Icon = (string)rts.Element("imageurl"),
VisibleStatus = (string)rts.Element("visiblestatus"),
AppStatus = (string)rts.Element("loginstatus"),
imgBubble =bitmapRed,
}).ToList<SampleCheckedData>();
this.lstImages.ItemsSource = interestrates;
private void btnAccept_MouseEnter(object sender, MouseEventArgs e)
{
int _id = int.Parse(((System.Windows.FrameworkElement)(e.OriginalSource)).Tag.ToString());
lstFriendRequuest.Items.RemoveAt(lstFriendRequuest.SelectedIndex);
}
To delete the selected item,
listbox.Items.RemoveAt(listbox.SelectedIndex);
Make your collection available globally on this page, and now you can manipulate on it easily from btnAccept_MouseEnter event:
public interestrates;
...
{
interestrates = ...
this.lstImages.ItemsSource = interestrates;
}
private void btnAccept_MouseEnter(object sender, MouseEventArgs e)
{
interestrates.RemoveAt(lstFriendRequuest.SelectedIndex);
}
Also, make sure that a click on a ListBox item changes SelectedIndex accordingly

Cant get selected listboxitem to show on my detail page

I have made and rss reader and have a listbox with all the feeds in it. When I select one item and want to see the whole article I only get the selectedindex number, 0,1,2 and so on.
Here is my code:
private void feedListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ListBox listBox = sender as ListBox;
if (listBox != null && listBox.SelectedItem != null)
{
NavigationService.Navigate(new Uri("/DetailsPage.xaml?feeditem=" + listBox.SelectedIndex, UriKind.Relative));
}
}
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
string feeditem = "";
if (NavigationContext.QueryString.TryGetValue("feeditem", out feeditem))
{
this.textBlock1.Text = feeditem;
}
}
What am I missing here?

How to format radgrid cell programmatically

I have to format (backcolor, forecolor, font style) a radgrid's cells depending on the value of the cell.
For example
If the value is negative set the fore color of that cell as red.
Can any one please tell me how this can be achieved?
protected void grdName_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
GridDataItem item = (GridDataItem)e.Item;
if (Convert.ToInt32(((DataRowView)item.DataItem)["Column"]) < value)
{
TableCell cell = item["Column"];
cell.BackColor = Color.PeachPuff;
}
}
}
Add the line onItemDataBound="Data_OnitemDataBound" to your radGrid declaration in your aspx page.
Then add this to your code behind. The number in the Cells[] is the index of the column you want to modify or validate against.
protected void Data_OnItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
GridDataItem item = (GridDataItem)e.Item;
if (Convert.ToDecimal(item.Cells[3].Text) < 0)
{
item.Cells[3].ForeColor = System.Drawing.Color.Red;
}
}
}
Below code can be used for all the cells in the RadGrid.
protected void RadGrid_ItemDataBound(object sender, GridItemEventArgs e)
{
foreach (GridDataItem dataItem in RadGridProduct.MasterTableView.Items)
{
int cellCount = dataItem.Cells.Count;
foreach (GridTableCell item in dataItem.Cells)
{
if (item.Text == null ||Convert.ToInt32(item.Text) < 0 )
item.BackColor = System.Drawing.Color.Brown;
}
}
}

PostBack and session values. Why after button is clicked and session created postback still show null

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
lblPostBack.Text = " Text created first time";
}
else
{
if (Session["Counter"] == null)
{
lblPostBack.Text = "PostBack x however strange becasue if is postback it's mean somebody clicked button and session value has been created";
}
else
{
lblPostBack.Text = "PostBack x should be count here";
}
}
}
protected void cmd_Click(object sender, EventArgs e)
{
int _counter;
if (Session["Counter"] == null)
{
_counter = 1;
}
else
{
_counter = (int)Session["Counter"] + 1;
}
Session["Counter"] = _counter;
lblPostBack.Text += "Counter: " + _counter.ToString();
}
Assuming this is ASP.NET: It's because the Click event on your button fires after the Load event on your page, so the session has not been set.
MSDN on the page lifecycle might be good reading - the button click is a "postback event" in the table in that document.
If I've got the wrong end of the stick, please explain what messages you get after the button clicks, and what you were expecting. Some framework and language tags on the question might not go amiss, either.
Ok it works, just FF mess up
I have added following method and works fine.
private int _counter;
protected void Page_Load(object sender, EventArgs e)
{
(...)
protected void Page_PreRender(Object sender, EventArgs e)
{
Session["Counter"] = _counter;
}

Resources