Telerik RadGrid with various types of edit controls

I need to send a list of fields. Some of them are editable, and some are not. Those that can be of different types, for example text fields, dates, time or logical. I am using Telerik RadGrid. See the screenshot for the test project that I did.

Questions: In the created element handler, why are there 4 cells? I had to fill cells [2] and cells [3] for the displayed values. I set AutoGenerateColumns to false, and I have only two GridTemplateColumn definitions.

Why does the item.RowIndex element overlap by two and increase to twenty? When I have 10 lines.

Is there a better way to do this?

This is how I declared the grid:

<telerik:RadGrid runat="server" ID="Grid1" Width="100%" Height="500px" GridLines="None" AutoGenerateColumns="false" OnItemCreated="OnGridItemCreated" OnNeedDataSource="OnGridNeedDataSource"> <ItemStyle Wrap="true" /> <AlternatingItemStyle Wrap="true" /> <ClientSettings> <Selecting AllowRowSelect="false" /> <Scrolling AllowScroll="true" UseStaticHeaders="true" /> <Resizing AllowColumnResize="true" ClipCellContentOnResize="true" ResizeGridOnColumnResize="true" EnableRealTimeResize="true" /> </ClientSettings> <MasterTableView> <NoRecordsTemplate> <asp:Label ID="lblNorec1" runat="server" Text="No records"></asp:Label> </NoRecordsTemplate> <Columns> <telerik:GridTemplateColumn UniqueName="FieldDisplayName" HeaderText="Field Name"> <ItemTemplate> </ItemTemplate> </telerik:GridTemplateColumn> <telerik:GridTemplateColumn UniqueName="FieldValue" HeaderText="Value"> <ItemTemplate> </ItemTemplate> </telerik:GridTemplateColumn> </Columns> </MasterTableView> </telerik:RadGrid> 

/

 / grid needs datasource protected void OnGridNeedDataSource(object source, GridNeedDataSourceEventArgs e) { // create a DataSource DataSet ds = new DataSet(); DataTable dt = new DataTable(); dt.Columns.Add(new DataColumn("Field_Display_Name")); dt.Columns.Add(new DataColumn("Field_Value")); for (int i = 0; i < 10; ++i) { DataRow dr = dt.NewRow(); dr["Field_Display_Name"] = "Item Id" + i.ToString(); dr["Field_Value"] = "Value" + i.ToString(); dt.Rows.Add(dr); } ds.Tables.Add(dt); Grid1.DataSource = ds; } // grid item is created protected void OnGridItemCreated(object sender, GridItemEventArgs e) { if (e.Item is GridDataItem) { GridDataItem item = (GridDataItem)e.Item; // fill in cells 2 and 3. Why are there 4? Label lbl = new Label(); lbl.Text = "Field " + item.RowIndex; item.Cells[2].Controls.Add(lbl); switch (item.RowIndex) { case 2: case 8: RadTextBox txt = new RadTextBox(); txt.ID = "RadTextBox1"; txt.Text = "hello " + item.RowIndex; item.Cells[3].Controls.Add(txt); break; case 4: case 10: RadDatePicker dp = new RadDatePicker(); dp.ID = "RadDatePicker1"; dp.SelectedDate = DateTime.Now; item.Cells[3].Controls.Add(dp); break; case 6: case 12: CheckBox cb = new CheckBox(); item.Cells[3].Controls.Add(cb); break; default: Label lbl2 = new Label(); lbl2.Text = "Value " + item.RowIndex; item.Cells[3].Controls.Add(lbl2); break; } } } 

alt text http://img193.imageshack.us/img193/1254/customgrid.png

+4
source share
1 answer

If the grid reflects your actual needs, I would suggest you go with an HTML table inside an ASP FormView. You can display most of the things you showed here right in the HTML. There are many FormView tutorials out there, it seems like what you need. Do not let the difficult attempt to squeeze this substance into a rad-read, you will go in the wrong direction.

If you want to show, for example, 10 records at a time, when gridview / radgrid is useful to you, if it's just one record at a time, look at the form.

EDIT: I just noticed that it was asked, like a year ago. I hope you have not yet waited for an answer to this question!

+1
source

All Articles