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

accessing Values in Cells, Dataitems cells and rows.

5 Answers 1029 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Andy
Top achievements
Rank 1
Andy asked on 04 Dec 2008, 09:55 PM
I have read over several times and searched the site for references but cannot seem to figure this one out<g>...

I have a row, with one editable field, qtyordered, I can access the values of the quantity supposedadly from the code below with the radnumeric text box... 

Along with this value, I need additional field values from the row....

according to your docs,  

TableCell cell = dataItem["UniqueName"] is the method to define it , then 
 cell["UniqueName"].text will get the value,

your example though<g> doesn't show a definition for dataItem[] .... and I cannot seem to find a way to get that ..

so, below is the method, I want to access the data in the row for the column unique name..

Thanks for your help....

 protected void radGrid1_QtyChanged(object sender, EventArgs e)
    {
        string UserSysID,
            PartID;
        decimal UnitPrice,
                ExitPrice;

          TableCell cell = dataItem["r1_PartNumber"];
   
            RadNumericTextBox qtyOrdered = (RadNumericTextBox)item.FindControl("qtyorderedRadNumericTextBox");
            string qOrd = qtyOrdered.Text;
            PartID = cell["r1_PartNumber"].text;
    }

Andy

5 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 05 Dec 2008, 06:15 AM
Hi Andy,

You can access an item in RadGrid as GridDataItem when the Grid is normal mode and as GridEditableItem when the Grid is in edit mode.

CS:
 
 protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)  
    {  
        //when the Grid is in normal mode  
        if(e.Item is GridDataItem)  
        {  
          GridDataItem dataitem=(GridDataItem)e.Item;  
          TableCell cell=dataitem["ProductName"];  
          string cellcelltext = cell.Text;  
  
        }  
  
         //when the Grid is in Edit mode  
        if ((e.Item is GridEditableItem) && (e.Item.IsInEditMode))  
        {  
            GridEditableItem edititem = (GridEditableItem)e.Item;  
            TextBox txtbx = (TextBox)edititem["ProductName"].Controls[0];  
        }  
    }  

Thanks
Shinu.
0
Andy
Top achievements
Rank 1
answered on 05 Dec 2008, 02:49 PM
I am not updating the record, so the ItemDatabound only fires when it first runs, and fills data..

I have setup a editable field for the qty, that fires the included method. I NEED to access the other fields from in there, the record in the grid is display readonly, and doesn't get updated.

thanks.

andy.
0
Andy
Top achievements
Rank 1
answered on 05 Dec 2008, 02:52 PM
ALSo this is fired from a TextChanged Event..

andy
0
Shinu
Top achievements
Rank 2
answered on 08 Dec 2008, 11:11 AM
Hi Andy,

From what I understood you are using a GridTemplateColumn with a RadNumericTextBox in the ItemTemplate and in the TextChanged event of the numerictextbox you are trying to access the rest of the cell values of the corresponding row. If so try the following code snippet to achieve the desired scenario.

ASPX:
<telerik:GridBoundColumn DataField="ProductName" UniqueName="ProductName" HeaderText="ProductName" ></telerik:GridBoundColumn> 
                    <telerik:GridTemplateColumn UniqueName="TempCol"  HeaderText="TempCol"   > 
                          <ItemTemplate> 
                              <telerik:RadNumericTextBox ID="RadNumericTextBox1" AutoPostBack="true" runat="server" OnTextChanged="RadNumericTextBox1_TextChanged"
                              </telerik:RadNumericTextBox> 
                          </ItemTemplate> 
                        </telerik:GridTemplateColumn> 
                    </Columns> 

CS:
protected void RadNumericTextBox1_TextChanged(object sender, EventArgs e) 
    { 
        RadNumericTextBox txtbx = (RadNumericTextBox)sender; 
        GridDataItem item = (GridDataItem)txtbx.NamingContainer; 
        string strTxt = item["ProductName"].Text.ToString(); 
    } 


Thanks
Shinu.
0
Andy
Top achievements
Rank 1
answered on 08 Dec 2008, 02:35 PM
Example code, for everyones reference.

Background, 

Query brings up set of parts from a Table Function of MSSQL based on a Type.....

users wishes to enter a quantity to order, without clicking any edit/insert/delete functionality in the grid.

fields created are:  PartNumber, Description, QtyinStock, QtyOrdered

All Fields are Readonly, exception is QtyOrdered as a template with OnTextChanged="radGrid1_QtyChanged"

example:

<telerik:GridTemplateColumn AllowFiltering="False" DataField="qtyordered" DataType="System.Int32"
                                        HeaderText="Qty Ordered" UniqueName="r1_QtyOrdered">
                                        <EditItemTemplate>
                                            <telerik:RadNumericTextBox ID="r1_QtyOrdered" runat="server" Type="Number" MinValue="0" 
                                                            MaxValue="9999" NumberFormat-DecimalDigits="0" Skin="Office2007" 
                                                            OnTextChanged="radGrid1_QtyChanged" AutoPostBack="true"> 
                                            </telerik:RadNumericTextBox>
                                        </EditItemTemplate>
                                    </telerik:GridTemplateColumn>


Once the user enters the qty ,

  protected void radGrid1_QtyChanged(object sender, EventArgs e)
    {

       
        foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
        {
            RadNumericTextBox qtyOrdered = (RadNumericTextBox)item.FindControl("r1_QtyOrdered");
            string qOrd = qtyOrdered.Text; // check for quantityy
            item.FireCommandEvent("Update", String.Empty);  // fire the update command
        }
    }

Update is fired and 

   protected void RadGrid1_UpdateCommand(object source, GridCommandEventArgs e)
    {

        GridEditableItem editedItem = e.Item as GridEditableItem;
        string iPartNumber = (editedItem["r1_PartNumber"].Controls[0] as TextBox).Text; 

        RadNumericTextBox qtyOrdered = (RadNumericTextBox)editedItem.FindControl("r1_QtyOrdered");
        string qOrd = qtyOrdered.Text;

        try
        {
            string sqlStatement;
            sqStatement = "Exec Dbo.InsertOrder "+ iPartNumber + "," + qOrd 
            SqlConnection Conn = new SqlConnection("Data Source="connectioninfo");
            SqlCommand cmd = new SqlCommand(sqlStatement, Conn);
            Conn.Open();
            cmd.ExecuteNonQuery();
        }
        catch(Exception ex)
        {
            RadGrid1.Controls.Add(new LiteralControl("Unable to insert order. Reason: " + ex.Message));
            e.Canceled = true;
        }
           

and it works. 

thank you ....

may want to put this out cleaned up(you know more than I do on coding the telerik).   

to me this is a simple order/detail type function.

andy




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