ASPxGridView - how to simply add sample values ​​with only the DataSource property?

Hello, I have an ASPxGridView. In it (for the uninformed) there is only a DataSource property to tell which data to load. My problem is that I'm just trying to mock an example and don't need to bind it to a real database. How should I do it? I basically just want some rows and several columns, but since this only requires a data source, I am not sure how to do this. Will ObjectDataSource be what I'm looking for?

+3
source share
2 answers

Just set the data source to a list of something 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>
+2

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

Source: https://habr.com/ru/post/1746283/


All Articles