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

RadGrid & ObjectDataSource

2 Answers 230 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ahmad
Top achievements
Rank 1
Ahmad asked on 10 Sep 2008, 10:07 AM
I'm facing an issue with databinding RadGrid to an ObjectDataSource, which admittedly I'm rather new at.

Users fill out a simple form and then click a button. This button should create a new object, and add it to a stongly-typed collection (list) of objects.

  protected void btnAddCustomer_Click(object sender, EventArgs e)  
    {  
        Customers custs = (Customers)Session["Customers"];  
        Customer cust = new Customer();  
        cust.CustomerID = this.cbCustomerList.SelectedValue;  
        custs.Add(cust);  
        Session["Customers"] = custs;          
        this.rgCustomers.Databind();         
 
    } 

I was supposing that after the call to Databind, the RadGrid should update with the current customer record, but it doesn't. "No Records to Display" remains there perpetual. (I am using RadAjaxManager, so that the button updates the radgrid)

So what am I missing? The Add method does work. but the objectdatasource doesn't seem to be doing the job.

<asp:ObjectDataSource ID="odsCustomers" runat="server"   
DataObjectTypeName="EClassLibrary.Customer" InsertMethod="Add"   
SelectMethod="Select" TypeName="EClassLibrary.Customers">  
</asp:ObjectDataSource> 

 public DataTable Select()  
        {  
            DataTable myTable = new EDataSet.dtCustomersDataTable();  
            EDataSet.dtCustomersRow myRow = (EDataSet.dtCustomersRow) myTable.NewRow();  
 
            foreach (Customer cust in this)  
            {  
                myRow.CustomerName = prov.GetCustomerName();  
                myRow.Company = prov.GetCustomerCompany();  
                myRow.JobTitle = prov.GetCustomerJobTitle();  
                myRow.UserID = prov.GetProviderUserID();  
                myTable.Rows.Add(myRow);                 
            }  
            return myTable;  
        } 

Someone please point out my begginer's mistake and put me out of my misery. Thanks in advance.


2 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 10 Sep 2008, 10:25 AM
Hello Ahmad,

How are you binding the grid? Assign the DataSource and then bind the grid as shown below:
protected void btnAddCustomer_Click(object sender, EventArgs e)   
    {   
        Customers custs = (Customers)Session["Customers"];   
        Customer cust = new Customer();   
        cust.CustomerID = this.cbCustomerList.SelectedValue;   
        custs.Add(cust);   
        Session["Customers"] = custs;           
        this.rgCustomers.DataSource=custs
        this.rgCustomers.Databind();       
  
    }  

Shinu.
0
Ahmad
Top achievements
Rank 1
answered on 10 Sep 2008, 10:47 AM
Hi Shinu,

Thanks for the quick reply. Although what you gave me does update the RadGrid, I am using an ObjectDataSource, and binding the RadGrid through DataSourceID

i.e.

<telerik:RadGrid ID="rgCustomers" runat="server" GridLines="None" DataSourceID="odsCustomers" AutoGenerateColumns="False">  
        <MasterTableView DataSourceID="odsCustomers">  
            <RowIndicatorColumn> 
                <HeaderStyle Width="20px" /> 
            </RowIndicatorColumn> 
            <ExpandCollapseColumn> 
                <HeaderStyle Width="20px" /> 
            </ExpandCollapseColumn> 
        </MasterTableView> 
        <FilterMenu EnableTheming="True">  
            <CollapseAnimation Duration="200" Type="OutQuint" /> 
        </FilterMenu> 
    </telerik:RadGrid> 

So shouldn't this work with the ObjectDataSource directly, instead of me having to specifiy the data source programmatically? Is the issue because I am adding/inserting records to the RadGrid in another control's event?
Tags
Grid
Asked by
Ahmad
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Ahmad
Top achievements
Rank 1
Share this question
or