Hi. I have to dispay a list of fields. Some are editable and some are not. Those that are can be different types, such as text fields, dates, times, or boolean. I'm using Telerik's RadGrid. See screen shot for a test project I made.
Questions: In the item created handler, why are there 4 cells? I had to fill in cells[2] and cells[3] for the values to display. I set AutoGenerateColumns to false, and I only have two GridTemplateColumn definitions.
Why does the item.RowIndex incrment by two's and go up to twenty? When I have 10 rows.
Is there a better way to do this?
Here's 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;
}
}
}