This is a migrated thread and some comments may be shown as answers.

AutoGenereated Columns Using Custom Controls to edit

1 Answer 122 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Shaun
Top achievements
Rank 1
Shaun asked on 11 Feb 2013, 02:08 PM
Hi
I have looked for a while now but have not come accross the same situation.
I have a grid with one Static Column and then other columns are generated when laoding the Datasource.

I would like to create a Custom Edit Template  acording to the data returned for each column as the number of columns can change.

Here is my RadGrid
<telerik:RadGrid ID="rgTVMTimes" runat="server" GridLines="None" OnItemCreated="rgCoinBox_ItemCreated"
              AllowMultiRowEdit="True" OnPreRender="rgTVMTimes_PreRender" OnColumnCreated="rgTVMTimes_ColumnCreated"
              OnItemDataBound="rgCoinBox_ItemDataBound" OnItemCommand="rgCoinBox_ItemCommand">
              <MasterTableView EditMode="InPlace" CommandItemDisplay="None" CommandItemStyle-HorizontalAlign="Right">
                <Columns>
                  <telerik:GridBoundColumn DataField="Heading" HeaderText="" ItemStyle-BackColor="LightGray"
                    SortExpression="Heading" UniqueName="Heading1" ReadOnly="true">
                      <filtertemplate>
                          <RadTimePiker ID="TVMTime"></RadTimePiker>
                      </filtertemplate>
                      <ItemStyle BackColor="LightGray" />
                  </telerik:GridBoundColumn>
                </Columns>
                <RowIndicatorColumn>
                  <HeaderStyle Width="20px"></HeaderStyle>
                </RowIndicatorColumn>
                <ExpandCollapseColumn>
                  <HeaderStyle Width="20px"></HeaderStyle>
                </ExpandCollapseColumn>
                <NoRecordsTemplate>
                  <p>
                    No TVM Machines found.
                  </p>
                  <p>
                    Choose new Location and try again.
                  </p>
                </NoRecordsTemplate>
                  <commanditemstyle horizontalalign="Right" />
              </MasterTableView>
            </telerik:RadGrid>
I have 2 of these grids.
I have defined The dataSource as a Select Query from the database to simplify the Data Transfer to the table.
rgTVMTimes.DataSource = new TVMCashUp().GetTvmMachineTimesGrid(_userInfo.StationID,"TVM Time");
 rgTVMTimes.DataBind();
rgTVMTimes.Rebind();
 
 
rgLogCash.DataSource = new TVMCashUp().GetTvmMachineTimesGrid(_userInfo.StationID, "Log Cash Review");
           rgLogCash.DataBind();
           rgLogCash.Rebind();
public DataTable GetTvmMachineTimesGrid(int inLocationid,string grid)
        {
            return DataMethods.ReturnMultipleRowResultSet("[HO].[GetTvmMachineTimesGrid]", inLocationid,grid);
        }
The one grid's Data is DBType.Bit valueTypes and the other a DBType.Time ValueType.
Here are the data returned by the query
Heading     TVM1        TVM2        TVM3        TVM4        TVM5        TVM6        TVM7
TVM Time    16:10:10    16:10:10    16:10:10    16:10:10    16:10:10    16:10:10    16:10:10

Heading            TVM1    TVM2    TVM3   TVM4   TVM5   TVM6    TVM7
Log Cash Review    0       0       0      0      0      0       0

I Would like to create a Yes No Radio Selection for the DBType.Bit grid and RadTimePiker for the DBType.Time Data.
I have found that when the data is in: 
    - a DBType.Date a datepicker is autogenerated. 
    - a DBType.Decimal a RadNumericTextbox is autogenerated.
    - a DBType.varchar a RadTextbox is autogenerated.

But I need these custom controls.

Is there a way I can create these templates. I edit the items inline.

Thanks in advance

1 Answer, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 13 Feb 2013, 10:57 AM
Hi,

One suggestion is to create the entire RadGrid from code behind. Set AutoGenerateColumns to false and create GridTemplateColumn form code behind so that you can customize the edit field as RadioButton. Please take a look into the following code snippet I tried.

ASPX:
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>

C#:
protected void Page_Init(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        string selectQuery1 = "SELECT  * FROM [BitTable]";
        SqlDataAdapter adapter1 = new SqlDataAdapter(selectQuery1, conn);
        conn.Open();
        adapter1.Fill(dt1);
        conn.Close();
    }
    RadGrid grid = new RadGrid();       
    grid.AutoGenerateColumns = false;
    grid.AutoGenerateEditColumn = true;      
    grid.DataSource = dt1;
         
    GridBoundColumn boundColumn1 = new GridBoundColumn();
    boundColumn1.DataField = "Id";
    boundColumn1.UniqueName = "Id";
    boundColumn1.HeaderText = "Id Column";
    grid.MasterTableView.Columns.Add(boundColumn1);
    for (int count = 1; count<dt1.Columns.Count; count++)
    {
 
        string templateColumnName = dt1.Columns[count].ColumnName;
        GridTemplateColumn templateColumn = new GridTemplateColumn();
        templateColumn.ItemTemplate = new MyTemplate(templateColumnName);
        templateColumn.EditItemTemplate = new MyEditItemTemplate(templateColumnName);
        templateColumn.HeaderText = templateColumnName;
        grid.MasterTableView.Columns.Add(templateColumn);
    }
    PlaceHolder1.Controls.Add(grid);     
}
private class MyTemplate : ITemplate
{
    protected Label Label;
    private string colname;
    public MyTemplate(string cName)
    {
        colname = cName;
    }
    public void InstantiateIn(System.Web.UI.Control container)
    {
 
        Label = new Label();
        Label.ID = colname + "Label";
        Label.DataBinding += new EventHandler(Label_DataBinding);
        container.Controls.Add(Label);
            
    }
 
    void Label_DataBinding(object sender, EventArgs e)
    {
        Label lbl = sender as Label;
        GridDataItem container = (GridDataItem)lbl.NamingContainer;
        lbl.Text = ((DataRowView)container.DataItem)[colname].ToString();
    }          
}
public class MyEditItemTemplate : IBindableTemplate
{
    RadioButton Radio1;
    RadioButton Radio2;      
    private string colname;
    public MyEditItemTemplate(string cName)
    {
        colname = cName;
    }
    public IOrderedDictionary ExtractValues(Control container)
    {
        OrderedDictionary dictionary = new OrderedDictionary();        
        return dictionary;
    }
    public void InstantiateIn(Control container)
    {
        Radio1 = new RadioButton();          
        Radio1.ID = colname + "_Radio1";
        Radio1.Text = "Yes";
        Radio2 = new RadioButton();
        Radio2.ID = colname + "_Radio2";
        Radio2.Text = "No";                     
        container.Controls.Add(Radio1);
        container.Controls.Add(Radio2);           
    }    
}

Thanks,
Shinu.
Tags
Grid
Asked by
Shaun
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Share this question
or