Add Column in GridView on Run Time using LINQ in ASP.Net

Last Reply 11 days ago By pandeyism

Posted 11 days ago

Add column run time.

so on run time i want to see the condition of row return and add another column RANK which i want to add in query result and bind to gridview so when page load gridview shows following column countryid (get from database) countryname (get from database) rank  (add on run time conditionally)

countryid  countrname

1            India

2            USA

3            China

var q = (from c in db.Countries
            select c).ToList();
string rank;
foreach (var item in q)
{
    if (item.Countryid == 1)
    {

        rank = "Top";
    }
    if (item.Countryid == 1)
    {

        rank = "second";
    }
}
GridView1.DataSource = q.ToList();
GridView1.DataBind();

 

Posted 11 days ago

Hi nauna,

Refer below sample.

HTML

<asp:GridView runat="server" ID="GridView1" AutoGenerateColumns="false" 
    onrowdatabound="GridView1_RowDataBound">
    <Columns>
        <asp:BoundField DataField="CustomerId" HeaderText="CustomerId" />
        <asp:BoundField DataField="ContactName" HeaderText="ContactName" />
        <asp:BoundField DataField="Country" HeaderText="Country" />
        <asp:TemplateField HeaderText="Rank">
            <ItemTemplate>
                <asp:Label ID="lblRank" runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        TestEntities entity = new TestEntities();
        var query = (from c in entity.TestCustomers
                        select c).ToList().Take(4);
        List<object> result = new List<object>();
        foreach (object item in query)
        {
            result.Add(item);
        }
        GridView1.DataSource = result;
        GridView1.DataBind();
    }
}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if (e.Row.Cells[0].Text == "AAFKM")
        {
            (e.Row.FindControl("lblRank") as Label).Text = "first";
        }
        else if (e.Row.Cells[0].Text == "ALFKI")
        {
            (e.Row.FindControl("lblRank") as Label).Text = "second";
        }
        else if (e.Row.Cells[0].Text == "ANATR")
        {
            (e.Row.FindControl("lblRank") as Label).Text = "third";
        }
        else if (e.Row.Cells[0].Text == "ANTON")
        {
            (e.Row.FindControl("lblRank") as Label).Text = "fourth";
        }
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        If Not Me.IsPostBack Then
            Dim entity As TestEntities = New TestEntities()
            Dim query = (From c In entity.TestCustomers Select c).ToList().Take(4)
            Dim result As List(Of Object) = New List(Of Object)()

            For Each item As Object In query
                result.Add(item)
            Next

            GridView1.DataSource = result
            GridView1.DataBind()
        End If
    End Sub

    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.DataRow Then

            If e.Row.Cells(0).Text = "AAFKM" Then
                (TryCast(e.Row.FindControl("lblRank"), Label)).Text = "first"
            ElseIf e.Row.Cells(0).Text = "ALFKI" Then
                (TryCast(e.Row.FindControl("lblRank"), Label)).Text = "second"
            ElseIf e.Row.Cells(0).Text = "ANATR" Then
                (TryCast(e.Row.FindControl("lblRank"), Label)).Text = "third"
            ElseIf e.Row.Cells(0).Text = "ANTON" Then
                (TryCast(e.Row.FindControl("lblRank"), Label)).Text = "fourth"
            End If
        End If
End Sub

Screenshot