2

I am creating a gridView that allows adding new rows by adding the controls necessary for the insert into the FooterTemplate, but when the ObjectDataSource has no records, I add a dummy row as the FooterTemplate is only displayed when there is data.

How can I hide this dummy row? I have tried setting e.row.visible = false on RowDataBound but the row is still visible.

spenibus
  • 4,339
  • 11
  • 26
  • 35
Nicholas
  • 1,740
  • 5
  • 22
  • 31

13 Answers13

4

You could handle the gridview's databound event and hide the dummy row. (Don't forget to assign the event property in the aspx code):

protected void GridView1_DataBound(object sender, EventArgs e)
    {
        if (GridView1.Rows.Count == 1)
            GridView1.Rows[0].Visible = false;
    }
John Miller
  • 999
  • 1
  • 9
  • 15
4

Please try the following

    protected void GridView1_DataBound(object sender, EventArgs e)
    {
        GridView1.Rows[0].Visible = false;
    }
Bo Persson
  • 90,663
  • 31
  • 146
  • 203
Harshal
  • 41
  • 1
1

I think this is what you need:

<asp:GridView ID="grid" runat="server" AutoGenerateColumns="false" ShowFooter="true" OnRowDataBound="OnRowDataBound">
    <Columns>
        <asp:TemplateField HeaderText="headertext">
            <ItemTemplate>
                itemtext
            </ItemTemplate>
            <FooterTemplate>
                insert controls
            </FooterTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

and the codebehind:

protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        e.Row.Attributes["style"] = "display:none";
    }
}

But I do not understand why you are adding your "insert controls" to the footer instead of placing them below the grid.

Serhat Ozgel
  • 23,496
  • 29
  • 102
  • 138
0

This is the incorrect usage of the GridView control. The GridView control has a special InsertRow which is where your controls should go.

Orion Adrian
  • 19,053
  • 13
  • 51
  • 67
  • I always want the footer shown as that is where my insert controls are contained, I want to hide the DummyRow I add. – Nicholas Sep 22 '08 at 12:41
0

Maybe try:

e.Row.Height = Unit.Pixel(0);

This isnt the right answer but it might work in the meantime until you get the right answer.

mattlant
  • 15,384
  • 4
  • 34
  • 44
0

Maybe use CSS to set display none?!

0

GridView has a special property to access Footer Row, named "FooterRow"

Then, you cold try yourGrid.FooterRow.Visible = false;

stefano m
  • 4,094
  • 5
  • 28
  • 27
0

I did this on a previous job, but since you can add rows, I always had it visible in the footer row. To make it so that the grid shows up, I bound an empty row of the type that is normally bound

dim row as Datarow = table.NewRow()
table.AddRow(row)
gridView.DataSource = table
gridView.Databind()

then it has all the columns and then you need. You can access the footer by pulling this:

'this will get the footer no matter how many rows there are in the grid.

Dim footer as Control = gridView.Controls(0).Controls(gridView.Controls(0).Controls.Count -1)

then to access any of the controls in the footer you would go and do a:

Dim cntl as Control = footer.FindControl(<Insert Control Name Here>)

I'd assume you'd be able to do a:

footer.Visible = false

to make the footer row invisible.

I hope this helps!

Edit I just figured out what you said. I basically delete the row when I add a new one, but to do this you need to check to see if there are any other rows, and if there are, check to see if there are values in it.

To delete the dummy row do something like this:

If mTable.Rows.Count = 1 AndAlso mTable.Rows(0)(<first column to check for null value>) Is DBNull.Value AndAlso mTable.Rows(0)(<second column>) Is DBNull.Value AndAlso mTable.Rows(0)(<thrid column>) Is DBNull.Value Then  
mTable.Rows.Remove(mTable.Rows(0))  
End If
mTable.Rows.Add(row)
gridView.Datasource = mTable
gridView.Databind()
Rob
  • 701
  • 3
  • 8
  • 21
0

Why are you not using the EmptyDataTemplate? It seems to work great even though I have only been using it for a couple days...

DiningPhilanderer
  • 2,737
  • 3
  • 25
  • 29
0

If you do not want to display data when the column/row is null:

if (!String.IsNullOrEmpty(item.DataName))
{
    e.Row.Visible = false;
}
spenibus
  • 4,339
  • 11
  • 26
  • 35
0

You should use DataKeyNames in your GridView:

<asp:GridView ID="GridView1" runat="server" DataKeyNames="FooID">

And then retrieve it on your code: GridView1.DataKeys[0].Value.ToString()

Where "0" is the number of the row you want to get the "FooID"

Davidson Sousa
  • 1,353
  • 1
  • 14
  • 34
0

To make it visible, just use:

Gridview.Rows.Item(i).Attributes.Add("style", "display:block")

And to make it invisible

Gridview.Rows.Item(i).Attributes.Add("style", "display:none")
-5

It can easily be done by SQL

USE YourdatabaseName select * from TableName where Column_Name <> ''
Barry Kaye
  • 7,682
  • 6
  • 42
  • 64
Anto SJ
  • 1
  • 1