Solved

Display / set text in gridview cell.

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

Suggested Solutions

Title # Comments Views Activity
.net and XML report from SCCM 5 20
mail.To.Add 5 25
Question about JQuery and asp.net 3 24
Help with preventing selection from a combobox 11 22
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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 Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
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…

776 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