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

Can I access columns from the datasource that are not in the grid?

1 Answer 109 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Dana Cobb
Top achievements
Rank 1
Iron
Dana Cobb asked on 19 Oct 2012, 12:27 PM
I have 2 columns in my datasource(not in the grid) that I want to access in order to format columns that I do have in the grid. I am applying my formatting in the ItemDataBound event.
Scenario:
Let's say my datasource has 4 columns. Select Field1, Field2, Field3, Field4 from myTable. In the grid I have Field1 with Unique column name "Field1" and Field2 with Unique column name "Field2". Can I access Field3 and Field4 from the datasource(not the grid) and format Field1 and Field2 with values that I analyze  in fields 3 and 4?
I know I can access Fields 3 and 4 if I put them in the grid with "display=false", but I fear this just adds more unecessary html in the grid(and I have 8 grids on the form). I seem to remember something about accessing the MasterTableView, but I can't find the post anywhere.

Thanks in advance,

Dana Cobb

1 Answer, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 22 Oct 2012, 05:16 AM
Hi,

Please take a look into following code snippet I tried.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" OnItemDataBound="RadGrid1_ItemDataBound" OnNeedDataSource="RadGrid1_NeedDataSource" AutoGenerateColumns="false">
    <MasterTableView>
        <Columns>
            <telerik:GridBoundColumn DataField="OrderId" UniqueName="OrderID">
            </telerik:GridBoundColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

C#:
protected void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
    string selectQuery1 = "select top 10 OrderID,CustomerID,EmployeeID from Orders";
    SqlDataAdapter adapter1 = new SqlDataAdapter(selectQuery1, conn);
    conn.Open();
    adapter1.Fill(dt1);
    conn.Close();
    RadGrid1.DataSource = dt1;
}
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        GridDataItem ditem = (GridDataItem)e.Item;
             
        foreach (DataRow row in dt1.Rows)
        {  
            string cellData = row["EmployeeID"].ToString();
            if (cellData == "2")
            {
                string order = row["OrderID"].ToString();
                if (ditem["OrderID"].Text == order)
                {
                    ditem["OrderID"].Text = "Hello";
                }
            }
        }
    }
}

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