locked
How to call Eval by column index? RRS feed

  • Question

  • User-1993844047 posted

    Hello,

    My resultset comes from a dynamic query, so that the column names are not known in advance.

    When binding those up with the GridView, I can't call Eval by name, how do I call them by column index?

    Thanks

    Jack

    Wednesday, April 22, 2015 6:07 AM

Answers

  • User61956409 posted

    Hi Jack,

    Thanks for your post.

    My resultset comes from a dynamic query, so that the column names are not known in advance.

    GridView could automatically generate columns if AutoGenerateColumns property is set to true. You could try to let your GridView automatically generate columns, this sample is for your reference.

    <div>
        <asp:Button ID="btnsource1" runat="server" Text="BindWithSource1" OnClick="btnsource1_Click" /><asp:Button ID="btnsource2" runat="server" Text="BindWithSource2" OnClick="btnsource2_Click" />
        <asp:GridView ID="GridView1" runat="server"></asp:GridView>
    </div> 
    
    protected void btnsource1_Click(object sender, EventArgs e)
    {
        BindWithSource1();
    }
    
    private void BindWithSource1()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("ID");
        dt.Columns.Add("Name");
        dt.Columns.Add("Age");
    
        dt.Rows.Add(1, "Tom", 28);
        dt.Rows.Add(2, "Jerry", 25);
        dt.Rows.Add(3, "Simmons", 30);
    
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
    
    protected void btnsource2_Click(object sender, EventArgs e)
    {
        BindWithSource2();
    }
    
    private void BindWithSource2()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("ID");
        dt.Columns.Add("Price");
        dt.Columns.Add("Num");
        dt.Columns.Add("Total");
    
        dt.Rows.Add(1, 15, 20,300);
        dt.Rows.Add(2, 100, 2, 200);
    
        GridView1.DataSource = dt;
        GridView1.DataBind();
    } 
    

    Best Regards,

    Fei Han



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 23, 2015 1:09 AM

All replies

  • User724169276 posted

    , how do I call them by column index?

    You can't.You can use DataItem here for explicit casting see https://support2.microsoft.com/default.aspx?scid=kb;en-us;307860.

    Wednesday, April 22, 2015 8:04 AM
  • User61956409 posted

    Hi Jack,

    Thanks for your post.

    My resultset comes from a dynamic query, so that the column names are not known in advance.

    GridView could automatically generate columns if AutoGenerateColumns property is set to true. You could try to let your GridView automatically generate columns, this sample is for your reference.

    <div>
        <asp:Button ID="btnsource1" runat="server" Text="BindWithSource1" OnClick="btnsource1_Click" /><asp:Button ID="btnsource2" runat="server" Text="BindWithSource2" OnClick="btnsource2_Click" />
        <asp:GridView ID="GridView1" runat="server"></asp:GridView>
    </div> 
    
    protected void btnsource1_Click(object sender, EventArgs e)
    {
        BindWithSource1();
    }
    
    private void BindWithSource1()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("ID");
        dt.Columns.Add("Name");
        dt.Columns.Add("Age");
    
        dt.Rows.Add(1, "Tom", 28);
        dt.Rows.Add(2, "Jerry", 25);
        dt.Rows.Add(3, "Simmons", 30);
    
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
    
    protected void btnsource2_Click(object sender, EventArgs e)
    {
        BindWithSource2();
    }
    
    private void BindWithSource2()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("ID");
        dt.Columns.Add("Price");
        dt.Columns.Add("Num");
        dt.Columns.Add("Total");
    
        dt.Rows.Add(1, 15, 20,300);
        dt.Rows.Add(2, 100, 2, 200);
    
        GridView1.DataSource = dt;
        GridView1.DataBind();
    } 
    

    Best Regards,

    Fei Han



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 23, 2015 1:09 AM