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

InsertCommand

3 Answers 206 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jagat
Top achievements
Rank 1
Jagat asked on 17 Feb 2012, 09:20 PM
HI,

I need to add a RequiredFieldValidator to the grid. The columns are autogenerated. 
I was able to acess the column value in the InsertCommand untill I added the below code to the ItemDataBound.
How do I acess the columns in the InsertCommand?

Thanks for your help!!

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
 
       {
 
 if ((e.Item is GridEditableItem && e.Item.IsInEditMode))
 
           {
 
               GridEditableItem item = e.Item as GridEditableItem;
 
               GridTextBoxColumnEditor editor = (GridTextBoxColumnEditor)item.EditManager.GetColumnEditor(Convert.ToString(ViewState["ColName"]));
 
               TableCell cell = (TableCell)editor.TextBoxControl.Parent;
 
  
 
               RequiredFieldValidator validator = new RequiredFieldValidator();
 
               editor.TextBoxControl.ID = "txtValue";
 
               validator.ControlToValidate = editor.TextBoxControl.ID;
 
               validator.ErrorMessage = "*";
 
               cell.Controls.Add(validator);
 
  
 
      
 
           }
 
       }
 
  
 
protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
 
       {
 
           string tableName = rcbLkup.SelectedValue;
 
           GridDataInsertItem row = (GridDataInsertItem)e.Item;
 
  
 
           BLL_LkupMaintenance objLkup = new BLL_LkupMaintenance();
 
  
 
           objLkup.ColName = Convert.ToString(ViewState["ColName"]);
 
           //objLkup.LkupValue = row[objLkup.ColName].Text;
 
           objLkup.LkupValue = ((TextBox)row.Controls[4].Controls[0]).Text;
 
           objLkup.AddToLkupTable(tableName);
 
  
 
       }

<telerik:RadGrid ID="RadGrid1" runat="server" OnNeedDataSource="RadGrid1_NeedDataSource"
        CellSpacing="0" GridLines="None"
    onprerender="RadGrid1_PreRender" onupdatecommand="RadGrid1_UpdateCommand"
        oninsertcommand="RadGrid1_InsertCommand"
        onitemdatabound="RadGrid1_ItemDataBound" >
        <HeaderContextMenu CssClass="GridContextMenu GridContextMenu_Default">
        </HeaderContextMenu>
        <MasterTableView EditMode="InPlace" DataKeyNames="ID" CommandItemDisplay="Top" >
            <CommandItemSettings ExportToPdfText="Export to PDF"></CommandItemSettings>
            <RowIndicatorColumn FilterControlAltText="Filter RowIndicator column">
                <HeaderStyle Width="20px"></HeaderStyle>
            </RowIndicatorColumn>
            <ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column">
                <HeaderStyle Width="20px"></HeaderStyle>
            </ExpandCollapseColumn>
            <Columns>
                <telerik:GridEditCommandColumn ButtonType="ImageButton" FilterControlAltText="Filter EditCommandColumn column">
                </telerik:GridEditCommandColumn>
            </Columns>
            <EditFormSettings>
                <EditColumn FilterControlAltText="Filter EditCommandColumn column">
                </EditColumn>
            </EditFormSettings>
        </MasterTableView>
        <FilterMenu EnableImageSprites="False">
        </FilterMenu>
    </telerik:RadGrid>

3 Answers, 1 is accepted

Sort by
0
Casey
Top achievements
Rank 1
answered on 17 Feb 2012, 09:52 PM
Hi Jagat,

I would try accessing the columns in the InsertCommand by adding the following to your code.
protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
       {
           string tableName = rcbLkup.SelectedValue;
           GridDataInsertItem row = (GridDataInsertItem)e.Item;
           // for GridBoundColumns
           string val1 = row["yourColumnName"].Text;
            
           //for TextBox inside of a GridTemplateColumn  
           TextBox tb = row.FindControl("TextBox1") as TextBox;
           string val2 = tb.Text;
       }

I hope this helps!
Casey
0
Shinu
Top achievements
Rank 2
answered on 20 Feb 2012, 05:00 AM
Hello Jagat,

Check the following help documentation.
Validation

-Shinu.
0
Jagat
Top achievements
Rank 1
answered on 20 Feb 2012, 05:14 PM
Thank you Casey and Shinu,

I'm using only one RadGrid to bind different DataTables. So  I do not know the column names during the runtime.  In the ItemDataBound event, I'm saving the columnName in a ViewState and trying to access the value. But I'm getting '&nbsp' if I use row[objLkup.ColName].Text; . If I remove the code that adds ValidationControl, I'm able to get the value using ((TextBox)row.Controls[4].Controls[0]).Text;
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
      {
          if (e.Item is GridHeaderItem)
          {
              GridHeaderItem header = (GridHeaderItem)e.Item;
              ViewState["ColName"] = ((TableCell)header.Controls[4]).Text;
 
          }
  protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
      {
          string tableName = rcbLkup.SelectedValue;
          GridDataItem row = (GridDataItem)e.Item;
 
          BLL_LkupMaintenance objLkup = new BLL_LkupMaintenance();
 
          objLkup.ColName = Convert.ToString(ViewState["ColName"]);
          //objLkup.LkupValue = row[objLkup.ColName].Text;          
          objLkup.LkupValue = ((TextBox)row.Controls[4].Controls[0]).Text;
          objLkup.AddToLkupTable(tableName);
 
      }

 

 

 

 

 

Tags
Grid
Asked by
Jagat
Top achievements
Rank 1
Answers by
Casey
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Jagat
Top achievements
Rank 1
Share this question
or