How to Create function in Eval in Gridview in Asp.Net


We will know now how to bind Gridview from database and if we want to generate custom column in Gridview then we can set using Eval in Gridview and for that we have to set autogeneratedcolumn to False in Gridview. We can get data from DataTable and bind in Gridview from database. 

Here we will check if we want to change some value when we got data from database it could be string or numeric then we have to check if it will be like this then we have to check if condition then that value will be display on the Gridview. We can do it in Gridview Eval. 

Or else we can create function and with that function through we can check data what we got from Eval and Change value of that Data and Display in Gridview control. So let’s start how to achieve this functionality.

Step 1: Create table in database.

CREATE TABLE [dbo].[Status](
                [StatusId] [int] IDENTITY(1,1) NOT NULL,
                [StatusName] [varchar](50) NOT NULL,
                [StatusCode] [int] NOT NULL,
 CONSTRAINT [PK_Status] PRIMARY KEY CLUSTERED
(
                [StatusId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

Step 2: Design your Webpage like below.

<div>
  <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Horizontal">
<Columns>
<asp:BoundField DataField="StatusId" HeaderText="Status Id" ItemStyle-Width="90">
<ItemStyle Width="90px"></ItemStyle></asp:BoundField>
<asp:BoundField DataField="StatusName" HeaderText="Error Name" ItemStyle-Width="150">
<ItemStyle Width="150px"></ItemStyle>
</asp:BoundField>
<asp:TemplateField HeaderText="Status" ItemStyle-Width="100">
<ItemTemplate>
<asp:Label ID="L1" Text='<%# Status(Eval("StatusCode").ToString()) %>' runat="server" />
</ItemTemplate>
<ItemStyle Width="100px"></ItemStyle>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCC99" ForeColor="Black" />
<HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
<SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F7F7F7" />
<SortedAscendingHeaderStyle BackColor="#4B4B4B" />
<SortedDescendingCellStyle BackColor="#E5E5E5" />
<SortedDescendingHeaderStyle BackColor="#242121" />
</asp:GridView><br /></div>

Here you can see how I Have Create function in Gridview Eval for checking data and display data in Gridview. If you want to put your condition then you can set your own condition based on your requirement. Or you can create function like I have created in Gridview Eval.

Create ConnectionString and Cal in Webpage like

<connectionStrings>
    <add name="DBCS" connectionString="Data Source=ServerName;Initial Catalog=DatabaseName;Integrated Security=True" providerName="System.Data.SqlClient" />
  </connectionStrings>

Call in Webpage like below.

string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;

Step 3: Now go to Code behind Bind Gridview from database. As we know how to bind it from database using connection string and pass query for what we want from database and store in DataTable.

if (!this.IsPostBack)
            {
                using (SqlConnection con = new SqlConnection(CS))
                {
                    SqlCommand cmd = new SqlCommand("Select * from Status", con);
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    DataTable dt = new DataTable();
                    da.Fill(dt);
                    if (dt.Rows.Count > 0)
                    {
                        GridView1.DataSource = dt;
                        GridView1.DataBind();
                        GridView2.DataSource = dt;
                        GridView2.DataBind();
                    }
                }
            }

Step 4: Run Application and see output.

Related Posts

Previous
Next Post »

Thanks for comments.....