Link to home
Start Free TrialLog in
Avatar of Homer2040
Homer2040

asked on

Display / set text in gridview cell.

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

ASKER CERTIFIED SOLUTION
Avatar of Gyanendra Singh
Gyanendra Singh
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Homer2040
Homer2040

ASKER

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.
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.
gr8 to see that you got a solution ... happy coding