Changing the color of a cell in a datalist

Discussion in 'ASP .Net' started by sck10, Jan 10, 2007.

  1. sck10

    sck10 Guest

    Hello,

    I am populating a DataList with data from a stored procedure. What I am
    trying to do is change the color based on the one of the values that the
    stored procedure sends.

    When I try to use the method "protected void
    dlstTransformation_ItemDataBound(object sender, DataListItemEventArgs e)",
    I get the error: System.Data.Common.DbDataRecord' to type
    'System.Data.DataRowView'.

    Any help with this would be appreciated.

    sck10

    <asp:DataList id="dlstTransformation" Runat="Server"
    OnItemDataBound="dlstTransformation_ItemDataBound"
    GridLines="Both"
    CellPadding="1"
    CellSpacing="3"
    RepeatColumns="4"
    RepeatDirection="Horizontal">
    <ItemTemplate>
    <asp:Label ID="lblTransformation" runat="server" Text='<%#
    Eval("ServiceLink") %>' />
    </ItemTemplate>
    </asp:DataList></asp:panel>




    protected void Transformation(string str00, string str01, string str02,
    string str03, string str04)
    {
    string strCatchName = "Transformation";

    try
    {
    //'Open connection to database
    using(SqlConnection cnnSearch = new
    SqlConnection(strConnSQLFundTrack))
    {
    cnnSearch.Open();

    SqlCommand cmdSearch = new SqlCommand(str00, cnnSearch);
    cmdSearch.CommandType = CommandType.StoredProcedure;

    //'Declare Parameters
    cmdSearch.Parameters.AddWithValue("@strParm01", str01);
    cmdSearch.Parameters.AddWithValue("@strParm02", str02);
    cmdSearch.Parameters.AddWithValue("@strParm03", str03);
    cmdSearch.Parameters.AddWithValue("@strParm04", str04);

    SqlDataReader sdrSearch = cmdSearch.ExecuteReader();
    dlstTransformation.DataSource = sdrSearch;
    dlstTransformation.DataBind();

    //To re-read the data, close Reader and re-open
    sdrSearch.Close();
    sdrSearch = cmdSearch.ExecuteReader();
    if(sdrSearch.HasRows)
    {
    this.pnlTransformation.Visible = true;
    this.lblTransformation.Text = strCatchName;
    //this.lblServiceDscr.Text =
    this.lblServiceDscr.Text.Replace("\r\n", "<br/>").Trim();
    } //Loop


    } // end using

    } // end try

    catch(Exception ex)
    {
    this.AppCodeHidePanels();
    this.pnlMessage.Visible = true;
    this.lblMessageTitle.Text = "Stored Procedure Error";
    this.lblMessageText.Text =
    "<span class=BlkB>Problems with stored procedure:</span> " +
    "<span class=BlkB>" + strCatchName + "</span><br />" +
    ex.Message.ToString();
    }

    finally
    {
    // can place something here that will always be fire.
    }

    } // end void


    protected void dlstTransformation_ItemDataBound(object sender,
    DataListItemEventArgs e)
    {
    if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
    ListItemType.AlternatingItem)
    {
    System.Data.DataRowView drv = (DataRowView)(e.Item.DataItem);
    string strBackColor = drv.Row["StatusColor"].ToString();
    e.Item.BackColor = System.Drawing.Color.Black;
    }
    }
     
    sck10, Jan 10, 2007
    #1
    1. Advertising

  2. Hello Steve,

    For your scenario, since you bind the DataReader to the DataList rather
    than a Dataset/DataTable, you can not convert each DataItem(in
    ItemDataBound event) to DataRowview. DataRowView is only available in
    DataView(which is generated from DataTable).

    In your ItemDataBound code, you can convert the DataItem to "IDataRecord"
    interface and retrive column value from it. e.g.

    ===============
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs
    e)
    {
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
    ListItemType.AlternatingItem)
    {
    IDataRecord record = e.Item.DataItem as IDataRecord;

    int id = (int)record["CategoryID"];
    string name = (string)record["CategoryName"];

    Response.Write("<br/>id: " + id + ", name: " + name);
    }
    }
    =====================

    Hope this helps.

    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead



    ==================================================

    Get notification to my posts through email? Please refer to
    http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
    ications.



    Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
    where an initial response from the community or a Microsoft Support
    Engineer within 1 business day is acceptable. Please note that each follow
    up response may take approximately 2 business days as the support
    professional working with you may need further investigation to reach the
    most efficient resolution. The offering is not appropriate for situations
    that require urgent, real-time or phone-based interactions or complex
    project analysis and dump analysis issues. Issues of this nature are best
    handled working with a dedicated Microsoft Support Engineer by contacting
    Microsoft Customer Support Services (CSS) at
    http://msdn.microsoft.com/subscriptions/support/default.aspx.

    ==================================================



    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Steven Cheng[MSFT], Jan 11, 2007
    #2
    1. Advertising

  3. sck10

    sck10 Guest

    Thanks Steven,

    worked perfectly...


    "Steven Cheng[MSFT]" <> wrote in message
    news:...
    > Hello Steve,
    >
    > For your scenario, since you bind the DataReader to the DataList rather
    > than a Dataset/DataTable, you can not convert each DataItem(in
    > ItemDataBound event) to DataRowview. DataRowView is only available in
    > DataView(which is generated from DataTable).
    >
    > In your ItemDataBound code, you can convert the DataItem to "IDataRecord"
    > interface and retrive column value from it. e.g.
    >
    > ===============
    > protected void Repeater1_ItemDataBound(object sender,
    > RepeaterItemEventArgs
    > e)
    > {
    > if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
    > ListItemType.AlternatingItem)
    > {
    > IDataRecord record = e.Item.DataItem as IDataRecord;
    >
    > int id = (int)record["CategoryID"];
    > string name = (string)record["CategoryName"];
    >
    > Response.Write("<br/>id: " + id + ", name: " + name);
    > }
    > }
    > =====================
    >
    > Hope this helps.
    >
    > Sincerely,
    >
    > Steven Cheng
    >
    > Microsoft MSDN Online Support Lead
    >
    >
    >
    > ==================================================
    >
    > Get notification to my posts through email? Please refer to
    > http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
    > ications.
    >
    >
    >
    > Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
    > where an initial response from the community or a Microsoft Support
    > Engineer within 1 business day is acceptable. Please note that each follow
    > up response may take approximately 2 business days as the support
    > professional working with you may need further investigation to reach the
    > most efficient resolution. The offering is not appropriate for situations
    > that require urgent, real-time or phone-based interactions or complex
    > project analysis and dump analysis issues. Issues of this nature are best
    > handled working with a dedicated Microsoft Support Engineer by contacting
    > Microsoft Customer Support Services (CSS) at
    > http://msdn.microsoft.com/subscriptions/support/default.aspx.
    >
    > ==================================================
    >
    >
    >
    > This posting is provided "AS IS" with no warranties, and confers no
    > rights.
    >
     
    sck10, Jan 11, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Franck
    Replies:
    7
    Views:
    3,448
    Franck
    Oct 12, 2005
  2. Hal Vaughan
    Replies:
    5
    Views:
    29,970
  3. Chanman
    Replies:
    0
    Views:
    975
    Chanman
    Jun 19, 2008
  4. fig000
    Replies:
    0
    Views:
    4,566
    fig000
    Sep 6, 2004
  5. Kamaljeet Saini
    Replies:
    0
    Views:
    447
    Kamaljeet Saini
    Feb 13, 2009
Loading...

Share This Page