Solved

Display / set text in gridview cell.

Posted on 2008-10-24
4
2,355 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
ID: 22806363
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
ID: 22811694
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
ID: 22812989
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
ID: 22814881
gr8 to see that you got a solution ... happy coding
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
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…
A short film showing how OnPage and Connectwise integration works.

932 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

12 Experts available now in Live!

Get 1:1 Help Now