Solved

Display / set text in gridview cell.

Posted on 2008-10-24
4
2,352 Views
Last Modified: 2013-11-07
Experts,

I have a gridview I want to use to allow users to add a comment.
The gridview is 1 row by 3 columns, on page load I check the database to see if the user has already submitted a comment.
If not, the gridview is not displaying, it needs to display and the third column needs to display the text: "Not Submitted".
If the user has submitted a comment and wants to add to it, the gridview needs to display and the third column needs to display "Submitted".

Thanks.

H.
HTML:

<asp:GridView ID="gvComments" runat="server" AutoGenerateColumns="False" Width="380px" CssClass="wrapTable" ShowHeader="False" OnRowDataBound="gvComments_OnRowDataBound">

            <Columns>

                <asp:HyperLinkField ShowHeader="False" Text="Add Comments" NavigateUrl="~/comments.aspx" >

                    <ItemStyle HorizontalAlign="Left" Width="100px" />

                </asp:HyperLinkField>

                <asp:HyperLinkField ShowHeader="False" Text="Comments">

                    <ItemStyle HorizontalAlign="Left" Width="90px" />

                </asp:HyperLinkField>

                <asp:BoundField DataField="comments" />

            </Columns>

        </asp:GridView>

 

Code Behind:

(in Page Load()):

 query commentsQuery = new Select()....

 SqlDataReader dr = (SqlDataReader)commentsQuery....

 

 gvComments.DataSource = dr;

 gvComments.DataBind();

 

 dr.Close();

 

(in OnRowDataBound()):

    protected void gvComments_OnRowDataBound(object sender, GridViewRowEventArgs e)

    {

        if (e.Row.RowType == DataControlRowType.DataRow)

        {

            if ((e.Row.Cells[2].Text != "") || (e.Row.Cells[2].Text != null))

            {

                e.Row.Cells[2].Text = "Submitted.";

            }

            else

            {

                e.Row.Cells[2].Text = "NotSubmitted.";

            }

        }

    }

Open in new window

0
Comment
Question by:Homer2040
  • 2
  • 2
4 Comments
 
LVL 16

Accepted Solution

by:
Gyanendra Singh earned 500 total points
Comment Utility
do one thing .. convert your bound column to template column

<asp:BoundField DataField="comments" />

to
<ItemTemplate>
                        <asp:Label ID="comments" Text='<%# Eval("yrvalue") %

>' runat="server"></asp:Label>


and on GridView1_RowDataBound event put this

if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label myLabel = new Label();
            myLabel = (Label)e.Row.FindControl("comments");
            string rowValue = myLabel.Text;

            if (rowValue == null || rowValue.Length < 1)
            {
                myLabel.Text = "NotSubmitted";
            }
else
{
 myLabel.Text = "Submitted";
}
        }
0
 

Author Comment

by:Homer2040
Comment Utility
BondinASP,

Thanks for the reply.
That suggestion is working if there is already a comment record in the database table for the user. However, I also need to display the gridview if the user is logging in for the first time. Otherwise they don't get the option to add a comment.

So the second part of my original question is how to display the gridview when there is no database record yet?

I have tried revmoving the databinding on the "comments" field and setting the default label text, but the gridview still does not display.

Any ideas?

Thanks.

H.
0
 

Author Comment

by:Homer2040
Comment Utility
BondinASP,

Thanks again for the reply.
I got the displaying issue (when no data) resolved.
I checked the reader, if there were no rows, I added a dataset and created a blank value in a table with a single row. Then bound it to the gridview.
If there was data in the reader, I bound the reader instead.

H.
0
 
LVL 16

Expert Comment

by:Gyanendra Singh
Comment Utility
gr8 to see that you got a solution ... happy coding
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now