DataSourceが空の場合でも、(GridView内の)FooterTemplateを常に可視にする簡単な方法はありますか?データがなくても常にFooterTemplateを表示
答えて
内容にかかわらず常に表示したい場合は、フッタのhtmlをの外に置くことはできませんか?FooterTemplate
?
何らかの理由でオプションでない場合は、add an null row to your data source if it's emptyまたはsubclass the GridView
& override the default behaviourのいずれかを指定できます。
これは私が知っている唯一のオプションです(ただし、最後にGridView
を使用してからしばらくお待ちしていますが)。
私は実際にHTMLでそれをやって気にしません、問題は、私はGridViewの列の幅に合わせて列をしたいです。 データが存在するときに要約が表示され、項目テンプレートで「新規」ボタンが押されたとき、またはフッターが常に表示されるときに、挿入項目(フッターに実装されていることは何を意味するのかわかります)を表示します。 つまり、 *フッターを表示する方法があります(データがない場合)? *実際にこのnullデータ行のものは、getcha(私はEntityDataSourceを使用して、私はより複雑または不可能と思うすべて)です。ありがとうございました。 – Shimmy
空の行を使いたくないのですが、GridViewをサブクラス化してもダミーのデータはありません。あなたはsomtnを持っていますか? – Shimmy
2番目のリンク(http://mattberseth.com/blog/2007/07/how%5Fto%5Fshow%5Fheader%5Fand%5Ffooter.html)には、ShowFooterWhenEmptyプロパティを持つグリッドを持つためのコード例がいくつかあります。 – Alconja
私もこれに問題がありました。 Alconjaのリンクは大変役に立ちますが(Thanks Alconja)、GridView.FooterRowはnullを返します。私はフッターから新しいレコードを挿入するために必要です。
これは私の最終的な解決策です。グリッドが空であっても、フッターからデータを挿入できるようになりました。
GridViewExtended.cs(App_Codeフォルダー内のクラス):のaspxページで
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace YourNamespace
{
public class GridViewExtended : GridView
{
#region Public Properties
[Category("Behavior")]
[Themeable(true)]
[Bindable(BindableSupport.No)]
public bool ShowFooterWhenEmpty
{
get
{
if (this.ViewState["ShowFooterWhenEmpty"] == null)
{
this.ViewState["ShowFooterWhenEmpty"] = false;
}
return (bool)this.ViewState["ShowFooterWhenEmpty"];
}
set
{
this.ViewState["ShowFooterWhenEmpty"] = value;
}
}
#endregion
private GridViewRow _footerRow2;
public override GridViewRow FooterRow
{
get
{
GridViewRow f = base.FooterRow;
if (f != null)
return f;
else
return _footerRow2;
}
}
protected override int CreateChildControls(System.Collections.IEnumerable dataSource, bool dataBinding)
{
int rows = base.CreateChildControls(dataSource, dataBinding);
// no data rows created, create empty table if enabled
if (rows == 0 && (this.ShowFooterWhenEmpty))
{
// create the table
Table table = this.CreateChildTable();
DataControlField[] fields;
if (this.AutoGenerateColumns)
{
PagedDataSource source = new PagedDataSource();
source.DataSource = dataSource;
System.Collections.ICollection autoGeneratedColumns = this.CreateColumns(source, true);
fields = new DataControlField[autoGeneratedColumns.Count];
autoGeneratedColumns.CopyTo(fields, 0);
}
else
{
fields = new DataControlField[this.Columns.Count];
this.Columns.CopyTo(fields, 0);
}
if (this.ShowHeaderWhenEmpty)
{
// create a new header row
GridViewRow headerRow = base.CreateRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal);
this.InitializeRow(headerRow, fields);
// add the header row to the table
table.Rows.Add(headerRow);
}
// create the empty row
GridViewRow emptyRow = new GridViewRow(-1, -1, DataControlRowType.EmptyDataRow, DataControlRowState.Normal);
TableCell cell = new TableCell();
cell.ColumnSpan = fields.Length;
cell.Width = Unit.Percentage(100);
// respect the precedence order if both EmptyDataTemplate
// and EmptyDataText are both supplied ...
if (this.EmptyDataTemplate != null)
{
this.EmptyDataTemplate.InstantiateIn(cell);
}
else if (!string.IsNullOrEmpty(this.EmptyDataText))
{
cell.Controls.Add(new LiteralControl(EmptyDataText));
}
emptyRow.Cells.Add(cell);
table.Rows.Add(emptyRow);
if (this.ShowFooterWhenEmpty)
{
// create footer row
_footerRow2 = base.CreateRow(-1, -1, DataControlRowType.Footer, DataControlRowState.Normal);
this.InitializeRow(_footerRow2, fields);
// add the footer to the table
table.Rows.Add(_footerRow2);
}
this.Controls.Clear();
this.Controls.Add(table);
}
return rows;
}
}
}
、単に
<%@ Register TagPrefix="YourPrefix" Namespace="YourNamespace" %>
を追加し、<YourPrefix:GridViewExtended
で<asp:GridView
を置き換えます誰かを助けることを願っています。
新しいレコードを追加するオプションがあるフッター付きのグリッドビューがあったので、これを試しました。 フッターの[新規追加]ボタンをクリックすると、新しい行を追加するコントロールが表示されます。このコードは期待どおりに機能しません(おそらく、base.CreateChildControlsの呼び出しをコードに置き換えてidの値などを設定する必要があります)。 –
この問題の私にとっての問題は、グリッドが空のとき何らかの理由でRowDataBoundイベントがフッターに対して起動しないことです。そこにいくつかのドロップダウンを設定する必要があるので、これは致命的な欠陥です。 –
これは素晴らしいことです!ありがとう! – jazzBox
前述のコメントの1つとして、RowDataBoundイベントはフッターに対して発生しません。 addresses this issueという別のコードスニペットが見つかりましたが、フッターを表示するだけでなく、明示的に行を作成して(RowCreatedイベントを発生させて)、それをバインドします(RowDataBoundイベントを起動します)。
私はコードコンバータを使用して上記の参照コードをc#に変換し、少し微調整しました。私は、コードを踏みながらそれを打ち破ったときのコメントも含めました。 RowCreatedイベントとRowDataBoundイベントが発生しており、フッターにドロップダウンを設定できます。
using System.Linq;
using System.Web.UI.WebControls;
using System.ComponentModel;
namespace WebUI.Controls
{
//modified from https://stackoverflow.com/questions/3437581/show-gridview-footer-on-empty-grid
public class GridViewExtended : GridView
{
private GridViewRow _footerRow;
[DefaultValue(false), Category("Appearance"), Description("Include the footer when the table is empty")]
public bool ShowFooterWhenEmpty { get; set; }
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Browsable(false)]
public override GridViewRow FooterRow {
get {
if ((this._footerRow == null)) {
this.EnsureChildControls();
}
return this._footerRow;
}
}
protected override int CreateChildControls(System.Collections.IEnumerable dataSource, bool dataBinding)
{
//creates all the rows that would normally be created when instantiating the grid
int returnVal = base.CreateChildControls(dataSource, dataBinding);
//if no rows were created (i.e. returnVal == 0), and we need to show the footer row, then we need to create and bind the footer row.
if (returnVal == 0 && this.ShowFooterWhenEmpty) {
Table table = this.Controls.OfType<Table>().First<Table>();
DataControlField[] dcf = new DataControlField[this.Columns.Count];
this.Columns.CopyTo(dcf, 0);
//creates the footer row
this._footerRow = this.CreateRow(-1, -1, DataControlRowType.Footer, DataControlRowState.Normal, dataBinding, null, dcf, table.Rows, null);
if (!this.ShowFooter) {
_footerRow.Visible = false;
}
}
return returnVal;
}
private GridViewRow CreateRow(int rowIndex, int dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState, bool dataBind, object dataItem, DataControlField[] fields, TableRowCollection rows, PagedDataSource pagedDataSource)
{
GridViewRow row = this.CreateRow(rowIndex, dataSourceIndex, rowType, rowState);
GridViewRowEventArgs e = new GridViewRowEventArgs(row);
if ((rowType != DataControlRowType.Pager)) {
this.InitializeRow(row, fields);
} else {
this.InitializePager(row, fields.Length, pagedDataSource);
}
//if the row has data, sets the data item
if (dataBind) {
row.DataItem = dataItem;
}
//Raises the RowCreated event
this.OnRowCreated(e);
//adds the row to the gridview's row collection
rows.Add(row);
//explicitly binds the data item to the row, including the footer row and raises the RowDataBound event.
if (dataBind) {
row.DataBind();
this.OnRowDataBound(e);
row.DataItem = null;
}
return row;
}
}
}
- 1. データがなくてもテーブルのデータを表示する! Oracle
- 2. savedInstanceStateがnullでなくても、フラグメントにデータを表示できません。
- 3. ハイチャート - データがなくてもx軸の強制的なカテゴリを表示する
- 4. Balloontextはデータがなくてもまだ表示されています
- 5. セルを(最初のものではなく)常にUITableViewに表示します。
- 6. データがあってもng-repeatが表示されない
- 7. iOSでViewControllerが表示されなくてもメソッドを続行
- 8. WordPress:投稿がなくてもアーカイブを表示
- 9. サブメニューポイント(ただし内容)がなくてもTMENU.2を表示
- 10. データがテーブルにない場合でも列を表示する
- 11. CollecitonViewが常にデータを正しく表示するとは限りません
- 12. CSS Divの枠線が表示されなくても表示されます
- 13. context.Database.Existsは、データベースファイルがなくても常にtrueを返します
- 14. gridviewが正しくデータを表示していない
- 15. GridViewのFootertemplate
- 16. アイテムが何も表示されていないときにすべてのデータを表示
- 17. Gmailアドオンはメールがなくても表示されますか?
- 18. tableRowが表示されなくてもフラッシュする方法(JavaFx)
- 19. 結果がなくても、間にすべての日付を表示する
- 20. トランザクション終了後でもデータがグラフに表示されない
- 21. 、私はいくつかの特定のデータを表示している今、私が前に表示されたもの以外の データを表示したい
- 22. 約束を解決してもテンプレートにデータが表示されない
- 23. 表示名が短くてもenumを取得するには?
- 24. テキストが表示されなくなってから表示が遅くなる
- 25. MySQLサーバがなくなったhaProxyで常にエラーが表示される
- 26. 角度:表示されていないデータに基づくdiv表示
- 27. uisearchbarを常にuitableviewに表示しておく
- 28. データがない場合でもレポートを表示する
- 29. Android - 大量のレイアウトファイルがなくてもデータを表示する最適な方法
- 30. フォーカスだけでなく、常にbootstrap-datepickerを表示する
なぜあなたはこれを達成したいですか? –
私が以下にコメントしたことを見て、私はそれが必要なものを説明しました。 – Shimmy