Telerik radgridview dynamically adds and deletes rows and columns

Source: Internet
Author: User
 

Recently, we have been using a third-party control telerik, version 2011 Q1, and the display control radgridview, which is easier to use than javasird,

Control problems are also found.

1. Add rows

Use begininsert () in radgridview to dynamically Add the default row method to avoid use with showinsertrow.

1 // Add rows
2 radgridview. begininsert ();

Or use showinsertrow = true;

  1 radgridview.ShowInsertRow=true;

2. Delete rows

The row deletion method is also provided in radgridview. You need to use the telerik: radgridviewcommands. delete command in teleri,

At the same time, set commandparameter to bind rows

1 <telerik:GridViewColumn Header="Delete" >
2 <telerik:GridViewColumn.CellTemplate>
3 <DataTemplate>
4 <telerik:RadButton Content="Delete"
5 Command="telerik:RadGridViewCommands.Delete"
6 CommandParameter="{Binding}" />
7 </DataTemplate>
8 </telerik:GridViewColumn.CellTemplate>
9 </telerik:GridViewColumn>

 

  

  

 

The writing method is the same as the above:

1 <telerik: gridviewcolumn header = "delete">
2 <telerik: gridviewcolumn. celltemplate>
3 <datatemplate>
4 <telerik: radbutton content = "delete"
5 // call Method
6 click = "delrow_click"/>
7 </datatemplate>
8 </telerik: gridviewcolumn. celltemplate>
9 </telerik: gridviewcolumn>
1  private void DelRow_Click(object sender, RoutedEventArgs e)
2 {
3 var deletecommand = RadGridViewCommands.Delete as RoutedUICommand;
4 deletecommand.Execute(radgridview.CurrentItem, radgridview);
5 }

 

There are many more commands for radgridviewcommands. You can check them if you have time.

3. Get row data

Using this control, we found that the data is not obtained as in the DataGrid, and it cannot be understood at the beginning. After reading the document carefully, the acquisition method is really different.

Converts each item in radgridview. items to gridrowview.

Use the radgridview. itemcontainergenerator. containerfromitem () method

1 foreach (var item in radgridview.Items)
2 {
3 GridViewRow row = radgridview.ItemContainerGenerator.ContainerFromItem(item) as GridViewRow;
4 int level=Convert.ToInt32((row.Cells[0].Content as TextBox).Text);
5 double min=Convert.ToDouble((row.Cells[1].Content as TextBox).Text);
6 double max=Convert.ToDouble((row.Cells[2].Content as TextBox).Text);
7 Color c=(row.Cells[3].Content as RadColorPicker).SelectedColor;
8 ......
9 }

 

4. Set the default initial value of the new row.

To add a new row through the begininsert () method, you need to set the initial values of each column in each row. You need to write the method in the rowloaded method.

Radgrid_rowloaded (Object sender, telerik. Windows. Controls. gridview. rowloadedeventargs E)

1 private void radgridview_rowloaded (Object sender, telerik. Windows. Controls. gridview. rowloadedeventargs E)
2 {
3 // is returned by the title bar
4 If (E. Row is gridviewheaderrow)
5 return;
6
7 int rownumber = (sender as radgridview). Items. count;
8
9 // set the initial values of each column
10 (E. Row. cells [0]. content as textbox). Text = rownumber. tostring ();
11 // random color
12 random Rand = new random ();
13 byte r = convert. tobyte (RAND. Next (255 ));
14 byte G = convert. tobyte (RAND. Next (255 ));
15 byte B = convert. tobyte (RAND. Next (255 ));
16 (E. Row. cells [3]. content as radcolorpicker). selectedcolor = color. fromargb (180, R, G, B );
17}

This article from Chrysanthemum blog, original address: http://www.cnblogs.com/MR520/archive/2011/08/08/2130731.html

Telerik radgridview dynamically adds and deletes rows and columns

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.