Wednesday, December 20, 2023

GRIDVIEW ON ROW DATA BOUND EVENT

 Database Create 

Student : roll , name , city , cost 

Fix 6 Value  in Database Record 


====================================================================

Default.aspx Page 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="default.aspx.cs" Inherits="WebApplication1._default" %>


<!DOCTYPE html>


<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 

OnRowDataBound = "OnRowDataBound" ShowFooter="True">

    <Columns>

        <asp:BoundField DataField="roll" HeaderText="Roll" ItemStyle-Width = "100">

<ItemStyle Width="100px"></ItemStyle>

        </asp:BoundField>

        <asp:BoundField DataField="name" HeaderText="Name" ItemStyle-Width = "100">


<ItemStyle Width="100px"></ItemStyle>

        </asp:BoundField>


        <asp:BoundField DataField="city" HeaderText="Name" ItemStyle-Width = "100">

<ItemStyle Width="100px"></ItemStyle>

        </asp:BoundField>

        <asp:TemplateField HeaderText="City">


            <ItemTemplate>




                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("cost") %>'></asp:TextBox>

            </ItemTemplate>



        </asp:TemplateField>

    </Columns>

</asp:GridView>

    </form>

</body>

</html>

=====================================================================


C#  Coading  Concept 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data.SqlClient;


namespace WebApplication1
{
    public partial class _default : System.Web.UI.Page
    {
        SqlConnection cn;
        SqlCommand cm;
        SqlDataReader dr;

        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {

                display();
            }

        }



        protected void display()
        {

            string path = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\dell\Desktop\gridvcont\WebApplication1\WebApplication1\App_Data\Database1.mdf;Integrated Security=True";
            cn = new SqlConnection(path);
            cn.Open();


            string k = "select * from student";
            cm = new SqlCommand(k, cn);
            dr = cm.ExecuteReader();

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

            dr.Close();



        }
        protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
        {

            
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                
                TextBox tb = (TextBox)e.Row.FindControl("TextBox1");

                 TableCell cell = e.Row.Cells[2];

                string cityname = cell.Text;

                if (cityname == "NAGPUR")
                {

                    tb.BackColor = System.Drawing.Color.Red;
                }

                
            }

           

        }

    }
}

GRIDVIEW ON ROW DATA BOUND EVENT

 Database Create  Student : roll , name , city , cost  Fix 6 Value  in Database Record  ====================================================...