locked
Linq to SQL Datasource for Gridview RRS feed

  • Question

  • User-305496339 posted

    I understand that this is a solution using a predefined collection. But I was wondering how could I implement this solution using linq to sql with with a data context. Thanks

    GridView1.DataSource = Enumerable.Range(1, 5).Select(a => new
    {

    FirstName = String.Format("First Name {0}", a),
    LastName = String.Format"Last Name {0}", a),
    ShowButton = a.Equals(5)
    });
    GridView1.DataBind();

    Thursday, January 7, 2016 4:34 PM

Answers

  • User-271186128 posted

    Hi rkrex,


    GridView1.DataSource = Enumerable.Range(1, 5).Select(a => new
    {

    FirstName = String.Format("First Name {0}", a),
    LastName = String.Format"Last Name {0}", a),
    ShowButton = a.Equals(5)
    });
    GridView1.DataBind();

    According to your code, the ShowButton is a bool type. So, you could check it in the GridView RowDataBound method. You could refer to the following code:

                <asp:GridView ID="GridView1" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound" runat="server">
                    <Columns>
                        <asp:BoundField DataField="FisrtName" SortExpression="FisrtName" HeaderText="FisrtName" />
                        <asp:BoundField DataField="LastName" SortExpression="LastName" HeaderText="LastName" />
                        <asp:TemplateField HeaderText="ShowButton">
                            <ItemTemplate>
                                <asp:HiddenField ID="hid_IsShowButton" runat="server" Value='<%# Eval("ShowButton") %>' />
                                <asp:Button ID="Button1" runat="server" Text="Button" />
    </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>

    Code behind:

            protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    HiddenField hid = (HiddenField)e.Row.FindControl("hid_IsShowButton");
                    Button btn = (Button)e.Row.FindControl("Button1");
                    if (hid != null)
                    {
                        if (btn != null)
                        {
                            if (hid.Value.ToLower().Equals("false"))
                                btn.Visible = false;
                            else
                                btn.Visible = true;
                        }
                    }
                }
            }

    Besides, you could also try to use the following code in GridView:

     <asp:Button ID="Button2" runat="server" Text="Button" Visible='<%# Eval("ShowButton") %>' />

    Then, there is no need to use the GridView RowDataBound event.

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 8, 2016 2:34 AM

All replies

  • User-698989805 posted
    You can not do the string formatting or datatype cast directly in Linq. But above query could be written as:

    DemoEntities db = new DemoEntities();

    var con = (from c in db.TableName
    select new { c.FirstName, c.LastName}.ToArray();

    GridView1.DataSource = con;
    GridView1.DataBind();

    Do the string formatting in the front-end like the following:

    <asp: TemplateField >
    < ItemTemplate>
    <asp: Label ID = " lblID " runat="server" Text='<% #Eval("FirstName").ToString() %>'></asp:Label>
    </ ItemTemplate>
    </asp: TemplateField >
    Thursday, January 7, 2016 6:40 PM
  • User-305496339 posted

    Thnaks but what about controlling whether the button is displayed or not?  How would you recommend a best practice for handling that?And doesn't link allow you to fetch one row at a time so I can check if if its the last row which at that time I can show the button? Thanks !

    Thursday, January 7, 2016 7:40 PM
  • User-271186128 posted

    Hi rkrex,


    GridView1.DataSource = Enumerable.Range(1, 5).Select(a => new
    {

    FirstName = String.Format("First Name {0}", a),
    LastName = String.Format"Last Name {0}", a),
    ShowButton = a.Equals(5)
    });
    GridView1.DataBind();

    According to your code, the ShowButton is a bool type. So, you could check it in the GridView RowDataBound method. You could refer to the following code:

                <asp:GridView ID="GridView1" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound" runat="server">
                    <Columns>
                        <asp:BoundField DataField="FisrtName" SortExpression="FisrtName" HeaderText="FisrtName" />
                        <asp:BoundField DataField="LastName" SortExpression="LastName" HeaderText="LastName" />
                        <asp:TemplateField HeaderText="ShowButton">
                            <ItemTemplate>
                                <asp:HiddenField ID="hid_IsShowButton" runat="server" Value='<%# Eval("ShowButton") %>' />
                                <asp:Button ID="Button1" runat="server" Text="Button" />
    </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>

    Code behind:

            protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    HiddenField hid = (HiddenField)e.Row.FindControl("hid_IsShowButton");
                    Button btn = (Button)e.Row.FindControl("Button1");
                    if (hid != null)
                    {
                        if (btn != null)
                        {
                            if (hid.Value.ToLower().Equals("false"))
                                btn.Visible = false;
                            else
                                btn.Visible = true;
                        }
                    }
                }
            }

    Besides, you could also try to use the following code in GridView:

     <asp:Button ID="Button2" runat="server" Text="Button" Visible='<%# Eval("ShowButton") %>' />

    Then, there is no need to use the GridView RowDataBound event.

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 8, 2016 2:34 AM