Solved

Display / set text in gridview cell.

Posted on 2008-10-24
4
2,368 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

626 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