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
source share