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

Grid binding example has problem

5 Answers 248 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Dan
Top achievements
Rank 2
Dan asked on 24 Dec 2008, 07:27 PM

I am using the  example code at:
http://www.telerik.com/help/aspnet-ajax/grdconditionalimagedisplayingridcolumn.html
to try to disable/non-visible a GridButtonColumn based on the value of another column value.  However it is not working as advertised.  I'm creating my grid in the code-behind thus:

public void AddParentFields(RadGrid RadGrid1)
    {
        string SqlCmd = "exec dbo.wa_AddFields 'PTTICKETS'";
        DataAccess sda = new DataAccess();
        GridBoundColumn boundColumn;
        sda.RunSelectCmd(SqlCmd);

        foreach (DataRow dr in sda.dtResult.Rows)
        {
            if (dr["fieldtype"].ToString().Trim() == "HYPERLINK")
            {
                GridHyperLinkColumn hyperLinkColumn = new GridHyperLinkColumn();
                hyperLinkColumn.DataTextField = dr["datafield"].ToString().Trim();
                hyperLinkColumn.HeaderText = dr["header"].ToString().Trim();
                hyperLinkColumn.ItemStyle.HorizontalAlign = HorizontalAlign.Left;
                hyperLinkColumn.Display = Convert.ToBoolean(dr["visible"]);
                //hyperLinkColumn.DataNavigateUrlField = "sh_track_url";
                string[] fieldArray;
                fieldArray = new string[2];
                fieldArray[0] = "sh_track_url";
                fieldArray[1] = "pcktrackno";
                hyperLinkColumn.DataNavigateUrlFields = fieldArray;
                hyperLinkColumn.DataNavigateUrlFormatString = "{0}{1}";
                hyperLinkColumn.Target = "_blank";
                RadGrid1.MasterTableView.Columns.Add(hyperLinkColumn);
            }
            else
            {
                boundColumn = new GridBoundColumn();
                boundColumn.DataField = dr["datafield"].ToString().Trim();
                boundColumn.HeaderText = dr["header"].ToString().Trim();
                boundColumn.DataFormatString = dr["fieldformat"].ToString().Trim();
                boundColumn.ItemStyle.HorizontalAlign = HorizontalAlign.Left;
                boundColumn.Display = Convert.ToBoolean(dr["visible"]);
                boundColumn.HeaderButtonType = GridHeaderButtonType.TextButton;
                RadGrid1.MasterTableView.Columns.Add(boundColumn);
            }
        }
        GridButtonColumn buttonColumn = new GridButtonColumn();
        buttonColumn.ButtonType = GridButtonColumnType.PushButton;
        buttonColumn.Text = "Confirm Receipt";
        buttonColumn.CommandName = "ConfirmReceipt";
        buttonColumn.UniqueName = "ConfirmReceipt";
        RadGrid1.MasterTableView.Columns.Add(buttonColumn);

        boundColumn = new GridBoundColumn();
        boundColumn.DataField = "patient_verified";
        boundColumn.Display = false;
        RadGrid1.MasterTableView.Columns.Add(boundColumn);
    }
---------------------
Then I try to set the visibility of the 'ConfirmReceipt' button based on the value of the 'patient_verified' column with:
------------------------
private void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem dataItem = e.Item as GridDataItem;
            GridBoundColumn boundColumn = dataItem["patient_verified"].Controls[0] as GridBoundColumn;
            if (boundColumn.Value == 1)
            {
                dataItem["ConfirmReceipt"].Controls[0].Visible = false;
            }
        }
    }
------------------------
But, the errors are:
on e.item
Cannot convert type 'Telerik.Web.UI.GidItem' to 'Telerik.WebContrtols.GridDataItem' via a reference conversion....

and on dataItem["patient_verified"].
Cannot convert type 'Systenm.Web.UI.Control' to 'Telerik.WebControls.GridBoundColumn' via a reference conversion....

Can you help me?  Thanks.
Dan

5 Answers, 1 is accepted

Sort by
0
Dimo
Telerik team
answered on 29 Dec 2008, 02:58 PM
Hi Dan,

The first error is probably caused by the fact that you have "using" directives for both the Telerik.Web.UI namespace and Telerik.WebControls namespace. In this case, please specify the object types by appending the namespace to the type:

GridDataItem dataItem = e.Item as Telerik.Web.UI.GridDataItem;


The second error is caused by incorrect cast and incorrect logic. The following returns a TableCell object

dataItem["patient_verified"]

The table cells of bound columns do not have controls inside by default. You can check the value inside the table cell like this:

(dataItem["patient_verified"] as TableCell).Text


All the best,
Dimo
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Dan
Top achievements
Rank 2
answered on 29 Dec 2008, 04:15 PM
Dimo,

Your reply has helped me to fix my compile errors.  But, my RadGrid1_ItemDataBound routine is not being called.  Is there a grid property that I have to set to have RadGrid1_ItemDataBound called when the grid loads?  I do not see this in the documentation anywhere.

Thanks,
Dan
0
Dimo
Telerik team
answered on 29 Dec 2008, 04:20 PM
Hello Dan,

Well, you have to add:

<telerik:RadGrid OnItemDataBound="RadGrid1_ItemDataBound"  />

or whatever the handler name is.

Kind regards,
Dimo
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Dan
Top achievements
Rank 2
answered on 29 Dec 2008, 04:48 PM
Dimo,

Don't know why this is so hard for me.  I have:

using

 

Telerik.WebControls;

 

private

 

void DefineGridStructure()

 

{

 

     RadGrid RadGrid1 = new RadGrid();

 

     RadGrid1.ItemDataBound +=

new GridItemEventHandler(RadGrid1_ItemDataBound);

and the error is:
No overload for 'RadGrid1_ItemDataBound' matches delegate 'Telerik.WebControls.GridItemEventHandler'

Dan

 

0
Accepted
Dimo
Telerik team
answered on 29 Dec 2008, 04:55 PM
Hello Dan,

Are you aware which RadGrid version you are using?

If you are using the classic RadGrid for ASP.NET, then you should use the Telerik.WebControls namespace.

If you are using the new RadGrid for ASP.NET AJAX, then you should use the Telerik.Web.UI namespace.

I am afraid these two are currently mixed up.

If you still have problems, please open a support ticket and attach a runnable project.

All the best,
Dimo
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
Grid
Asked by
Dan
Top achievements
Rank 2
Answers by
Dimo
Telerik team
Dan
Top achievements
Rank 2
Share this question
or