2008-09-22 95 views
2

我創建一個gridView,允許通過添加必要的插入到FooterTemplate控件添加新行,但是當ObjectDataSource沒有記錄,我添加了一個虛擬的行爲FooterTemplate是一個GridView行只有在有數據時才顯示。隱藏在asp.net

如何隱藏此虛擬行?我試圖在RowDataBound上設置e.row.visible = false,但該行仍然可見。

回答

0

這是GridView控件的不正確用法。 GridView控件有一個特殊的InsertRow,它是你的控件應該去的地方。

+0

我總是想顯示爲頁腳是我的插入控件包含在哪裏,我想隱藏我添加的DummyRow。 – Nicholas 2008-09-22 12:41:32

0

也許嘗試:

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

這心不是正確的答案,但要等到找到正確答案,它可能會在此期間的工作。

+0

不起作用,仍然顯示空行:-( – Nicholas 2008-09-22 12:55:20

0

也許使用CSS來設置顯示沒有?

1

我想這是你所需要的:

<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> 

和代碼隱藏:

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

但我不明白爲什麼要添加你的「插入控件」的頁腳,而不是將他們在網格下面。

3

您可以處理gridview的數據綁定事件並隱藏虛擬行。 (不要忘了事件屬性在ASPX代碼分配):

protected void GridView1_DataBound(object sender, EventArgs e) 
    { 
     if (GridView1.Rows.Count == 1) 
      GridView1.Rows[0].Visible = false; 
    } 
0

GridView中有一個特殊的屬性來訪問頁腳行,名爲「FooterRow」

然後,你冷嘗試yourGrid.FooterRow。 Visible = false;

0

我在之前的工作上做過這件事,但既然可以添加行,我總是在頁腳行中看到它。爲了使網格顯示出來,我綁定了一個空行,通常是綁定的類型

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

然後它具有所有列,然後您需要。您可以通過拉動這個訪問頁腳:

'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) 

然後訪問任何控件的頁腳,你會去做一個:

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

我會假設你能夠做一個:

footer.Visible = false 

使腳註行不可見。

我希望這有助於!

編輯我剛想清楚你說了什麼。當我添加一個新行時,我基本上刪除了行,但要做到這一點,您需要檢查是否有其他行,如果有,請檢查是否有值。

要刪除虛擬行做這樣的事情:

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() 
0

使其可見,只需使用:

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

,並使其隱形

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

爲什麼你沒有使用EmptyDataTemplate?它似乎工作,即使我只用它幾天偉大的...

0

你應該在你的GridView使用的DataKeyNames:

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

,然後檢索它在你的代碼: GridView1.DataKeys[0].Value.ToString()

其中,「0」是你想要得到的「FooID」

3

請嘗試以下

行的數量
protected void GridView1_DataBound(object sender, EventArgs e) 
    { 
     GridView1.Rows[0].Visible = false; 
    } 
+0

這隱藏了我的整個GV。 – SearchForKnowledge 2015-03-25 13:18:49

-4

它可以很容易地通過SQL

USE YourdatabaseName select * from TableName where Column_Name <> '' 
+4

-1該問題與SQL無關,他正在嘗試使用GridView。 – JustinDoesWork 2012-03-23 19:44:57

0

做。如果你不想當行/列是空來顯示數據:

if (!String.IsNullOrEmpty(item.DataName)) 
{ 
    e.Row.Visible = false; 
}