ASPxGridView——如何仅使用 DataSource 属性简单地添加示例值?

发布于 2024-09-02 04:23:17 字数 176 浏览 6 评论 0原文

你好,我有一个 ASPxGridView。其中(对于不知情的人来说)只是一个 DataSource 属性,用于告诉它要加载哪些数据。我的问题是,我只是想模拟一个示例,不需要将其与实际的数据库联系起来。我该怎么做?我基本上只想要几行和一些列,但由于它只需要一个数据源,我不知道该怎么做。 ObjectDataSource 是我正在寻找的吗?

Hello I have a ASPxGridView. In it(for the uninformed) is only a DataSource property for telling it what data to load. My problem is that I'm simply trying to mock up an example and don't need to tie it to an actual database. How would I do this? I basically just want a few rows and some columns but since it only takes a datasource I'm not sure how to do it. Would ObjectDataSource be what I'm looking for?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故笙诉离歌 2024-09-09 04:23:17

只需将数据源设置为类似这样的列表:

public class Item
{
  public string Name { get; set; }
  public int Count { get; set; }
}

protected void Page_Load(object sender, EventArgs e)
{
  GridView1.DataSource = new Item[] { new Item { Name = "2", Count = 2 }, new Item { Name = "3", Count = 3 }, };
  GridView1.DataBind();
}


<dxwgv:ASPxGridView ID="grid" ClientInstanceName="grid" runat="server" Width="100%" AutoGenerateColumns="False" >
     <Columns>
         <dxwgv:GridViewDataTextColumn Caption="Name" FieldName="Name" ReadOnly="True">
         </dxwgv:GridViewDataTextColumn>
         <dxwgv:GridViewDataTextColumn Caption="Count" FieldName="Count" ReadOnly="True" >
         </dxwgv:GridViewDataTextColumn>
     </Columns>
     </dxwgv:ASPxGridView>

Just set the datasource to a list of anything like this:

public class Item
{
  public string Name { get; set; }
  public int Count { get; set; }
}

protected void Page_Load(object sender, EventArgs e)
{
  GridView1.DataSource = new Item[] { new Item { Name = "2", Count = 2 }, new Item { Name = "3", Count = 3 }, };
  GridView1.DataBind();
}


<dxwgv:ASPxGridView ID="grid" ClientInstanceName="grid" runat="server" Width="100%" AutoGenerateColumns="False" >
     <Columns>
         <dxwgv:GridViewDataTextColumn Caption="Name" FieldName="Name" ReadOnly="True">
         </dxwgv:GridViewDataTextColumn>
         <dxwgv:GridViewDataTextColumn Caption="Count" FieldName="Count" ReadOnly="True" >
         </dxwgv:GridViewDataTextColumn>
     </Columns>
     </dxwgv:ASPxGridView>
烟酒忠诚 2024-09-09 04:23:17

使用 DataTable 的另外两种方式:

    private DataTable getSampleDataSource1()
    {
        DataTable dtblResult = new DataTable();
        dtblResult.Columns.Add("Name");
        dtblResult.Columns.Add("Count");

        dtblResult.Rows.Add("Name1", "1");
        dtblResult.Rows.Add("Name2", "3");
        dtblResult.Rows.Add("Name3", "7");
        dtblResult.Rows.Add("Name4", "9");

        return dtblResult;
    }

    private DataTable getSampleDataSource2()
    {
        DataTable dtblResult = new DataTable();
        dtblResult.Columns.Add("Name");
        dtblResult.Columns.Add("Count");

        DataRow drow;
        drow = dtblResult.NewRow();
        dtblResult.Rows.Add(drow);
        drow.ItemArray = new object[] { "Name1", "1" };

        drow = dtblResult.NewRow();
        dtblResult.Rows.Add(drow);
        drow.ItemArray = new object[] { "Name2", "3" };

        drow = dtblResult.NewRow();
        dtblResult.Rows.Add(drow);
        drow.ItemArray = new object[] { "Name3", "7" };

        drow = dtblResult.NewRow();
        dtblResult.Rows.Add(drow);
        drow.ItemArray = new object[] { "Name4", "9" };

        return dtblResult;
    }

    private void setDataSource(ASPxGridView theGridView)
    {
        theGridView.KeyFieldName = "Name";
        theGridView.DataSource = getSampleDataSource1();
        theGridView.DataBind();
    }

Two other ways using DataTable:

    private DataTable getSampleDataSource1()
    {
        DataTable dtblResult = new DataTable();
        dtblResult.Columns.Add("Name");
        dtblResult.Columns.Add("Count");

        dtblResult.Rows.Add("Name1", "1");
        dtblResult.Rows.Add("Name2", "3");
        dtblResult.Rows.Add("Name3", "7");
        dtblResult.Rows.Add("Name4", "9");

        return dtblResult;
    }

    private DataTable getSampleDataSource2()
    {
        DataTable dtblResult = new DataTable();
        dtblResult.Columns.Add("Name");
        dtblResult.Columns.Add("Count");

        DataRow drow;
        drow = dtblResult.NewRow();
        dtblResult.Rows.Add(drow);
        drow.ItemArray = new object[] { "Name1", "1" };

        drow = dtblResult.NewRow();
        dtblResult.Rows.Add(drow);
        drow.ItemArray = new object[] { "Name2", "3" };

        drow = dtblResult.NewRow();
        dtblResult.Rows.Add(drow);
        drow.ItemArray = new object[] { "Name3", "7" };

        drow = dtblResult.NewRow();
        dtblResult.Rows.Add(drow);
        drow.ItemArray = new object[] { "Name4", "9" };

        return dtblResult;
    }

    private void setDataSource(ASPxGridView theGridView)
    {
        theGridView.KeyFieldName = "Name";
        theGridView.DataSource = getSampleDataSource1();
        theGridView.DataBind();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文