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

[Solved] ItemTemplate as ItemEditTemplate and adding new row

2 Answers 107 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Aleksei
Top achievements
Rank 1
Aleksei asked on 04 Aug 2009, 11:45 AM
Hello !

I needed a RadGrid with possibility to change data in it without pressing any Edit buttons, so i added textboxes and Save/Delete buttons directly to ItemTemplates, also added  CommandItemDisplay="Top" to insert new rows, as a result  RadGrid looks like this:

    <telerik:RadGrid ID="gvwEducations" runat="server" GridLines="None" OnItemDataBound="gvwEducations_ItemDataBound"
        AutoGenerateColumns="False" OnItemCommand="gvwEducations_ItemCommand">
        <MasterTableView CommandItemDisplay="Top" AutoGenerateColumns="false" EditMode="InPlace">
            <Columns>
                <telerik:GridTemplateColumn UniqueName="IntstituteTemplate" HeaderText="Institute">
                    <ItemTemplate>
                        <telerik:RadComboBox ID="cbxInstitute" runat="server">
                        </telerik:RadComboBox>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <telerik:RadComboBox ID="cbxInstitute" runat="server">
                        </telerik:RadComboBox>
                    </EditItemTemplate>
                </telerik:GridTemplateColumn>

                <telerik:GridTemplateColumn UniqueName="StartTimeTemplate" HeaderText="Start">
                    <ItemTemplate>
                        <telerik:RadTextBox ID="tbxStartYear" runat="server">
                        </telerik:RadTextBox>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <telerik:RadTextBox ID="tbxStartYear" runat="server">
                        </telerik:RadTextBox>
                    </EditItemTemplate>
                </telerik:GridTemplateColumn>

                <telerik:GridButtonColumn CommandName="Save" Text="Save" UniqueName="SaveColumn"
                    ShowInEditForm="true">
                </telerik:GridButtonColumn>
                <telerik:GridButtonColumn CommandName="Delete" Text="Delete" UniqueName="DeleteColumn"
                    ShowInEditForm="true">
                </telerik:GridButtonColumn>
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>

Here comes the questions :)
is it possible to use ItemTemplate on item insert ?(at the moment edititemtemplate is just copypaste of itemtemplate) 



and one more questions about codebehind :

        protected void gvwEducations_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridDataItem)
            {
                RadComboBox cbxInstitute = (RadComboBox)e.Item.FindControl("cbxInstitute");
                RadTextBox tbxStart = (RadTextBox)e.Item.FindControl("tbxStartYear");
                cbxInstitute.DataSource = SomeCacheDatasource;
   cbxInstitute.DataBind();

                if (e.Item.DataItem is MyDataType)
                {
//fill textboxes with data from datasource
                }
            }
            if (e.Item is GridEditFormInsertItem) //if inserting new item, fill comboboxes
            {
                RadComboBox cbxInstitute = (RadComboBox)e.Item.FindControl("cbxInstitute");
                cbxInstitute.DataSource = SomeCacheDatasource;
                cbxInstitute.DataBind();

            }
        }

        protected void gvwEducations_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == "Save")
            {
                SaveData(e);
            }
        }

when i save newly added item in gvwEducations_ItemCommand all it's controls values exist but they are empty,
am i doeing something wrong ?


Thank you !

2 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 04 Aug 2009, 01:18 PM
Hello Aleksei,

To populate the RadComboBox while inserting a new item, you have to try the following code
c#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
    { 
        if (e.Item is GridDataInsertItem && e.Item.OwnerTableView.IsItemInserted)//GridDataInsertItem since EditMode = "InPlace" 
        { 
            GridDataInsertItem editItem = (GridDataInsertItem)e.Item; 
            RadComboBox cbxInstitute = (RadComboBox)editItem.FindControl("cbxInstitute"); 
            cbxInstitute.DataSource = SomeCacheDatasource; 
            cbxInstitute.DataTextField = "DataField"
            cbxInstitute.DataBind(); 
        } 
  } 

Also while saving use the following code:
 private void SaveData(GridItemEventArgs e) 
    { 
        if (e.Item is GridDataInsertItem && e.Item.OwnerTableView.IsItemInserted) 
           { 
                  GridDataInsertItem editItem = (GridDataInsertItem)e.Item; 
                  RadComboBox cbxInstitute = (RadComboBox)editItem.FindControl("cbxInstitute"); 
                  string strtxt = cbxInstitute.SelectedItem.Text; 
           }   
    } 

Thanks
Shinu.
0
Aleksei
Top achievements
Rank 1
answered on 04 Aug 2009, 01:49 PM
Hello Shinu,

Thank You for reply !

 I've tryed the code, but unfortunately wasnt able to get cbxInstitute value in SaveData method.
(can it be due to i've added AllowCustomText="true" in cbxInstitute ?)

Thx.
Tags
Grid
Asked by
Aleksei
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Aleksei
Top achievements
Rank 1
Share this question
or