Solved

Display / set text in gridview cell.

Posted on 2008-10-24
4
2,360 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

How our DevOps Teams Maximize Uptime

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

Question has a verified solution.

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

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…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

809 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