locked
Check Column Data And Set Yes No In New Column RRS feed

  • Question

  • User-807418713 posted

    Hello

    This is my code

    <%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="TESTFile.aspx.cs" Inherits="TESTFile" Title="Untitled Page" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
     <asp:GridView ID="GridView1" runat="server" Font-Bold="False"
                Font-Names="Calibri" Font-Size="12pt" AutoGenerateColumns="False" >
                <Columns>
                    <asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID">
                        <ItemStyle HorizontalAlign="Left" ForeColor="Black" Wrap="True" Width="100px" />
                    </asp:BoundField>
                    <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name">
                        <ItemStyle HorizontalAlign="Left" ForeColor="Black" Wrap="True" Width="100px" />
                    </asp:BoundField>
                    <asp:TemplateField HeaderText="Qty">
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("Qty") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="L1" runat="server" Text='<%# Bind("Qty") %>'></asp:Label>
                        </ItemTemplate>
                        <ItemStyle ForeColor="Black" HorizontalAlign="Right" Width="100px" Wrap="True" />
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Rate">
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("Rate") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="L2" runat="server" Text='<%# Bind("Rate") %>'></asp:Label>
                        </ItemTemplate>
                        <ItemStyle ForeColor="Black" HorizontalAlign="Right" Width="100px" Wrap="True" />
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Name2">
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("Name2") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="L3" runat="server" Text='<%# Bind("Name2") %>'></asp:Label>
                        </ItemTemplate>
                        <ItemStyle ForeColor="Black" HorizontalAlign="Right" Width="100px" Wrap="True" />
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
    
     
    </asp:Content>
    
    

    code behind example

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;
    using System.Collections.Specialized;
    using System.Text;
    using System.Drawing;
    using System.IO;
    using System.Net;
    using System.Net.Mail;
    using System.Net.Configuration;
    public partial class TESTFile : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataTable dt = new DataTable();
                dt.Columns.AddRange(new DataColumn[5] { new DataColumn("ID"), new DataColumn("Name"), new DataColumn("Qty"), new DataColumn("Rate"), new DataColumn("Name2") });
                dt.Rows.Add(1, "AA", 10, 1, "AA");
                dt.Rows.Add(2, "BB", 20, 2, "B");
                dt.Rows.Add(3, "CC", 5, 5, "V");
                dt.Rows.Add(4, "DD", 10, 5, "DD");
                dt.Rows.Add(4, "EE", 10, 5, "EE");
                dt.Rows.Add(4, "F", 10, 5, "FF");
    
               
                GridView1.DataSource = dt;
                GridView1.DataBind();
                
            }
        }
    
       
    
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
           
        }
    
    
        
     
    }
    

    it showing result set like this

    but i want like this

    how to do automatically

    Thanking You

    Monday, October 7, 2019 6:33 PM

Answers

  • User288213138 posted

    Hi Gopi.MCA,

    how to do automatically

    According to your description, i made demo for your.

    I bulided a TemplateField in aspx firstly, then set the value of TemplateField by comparing the values of name and name2.

    The code:

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound" >
                    <Columns>
                        <asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID">
                            <ItemStyle HorizontalAlign="Left" ForeColor="Black" Wrap="True" Width="100px" />
                        </asp:BoundField>
    
                        <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name">
                            <ItemStyle HorizontalAlign="Left" ForeColor="Black" Wrap="True" Width="100px" />
                        </asp:BoundField>
    
                        <asp:TemplateField HeaderText="Qty">
                            <EditItemTemplate>
                                <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("Qty") %>'></asp:TextBox>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="L1" runat="server" Text='<%# Bind("Qty") %>'></asp:Label>
                            </ItemTemplate>
                            <ItemStyle ForeColor="Black" HorizontalAlign="Right" Width="100px" Wrap="True" />
                        </asp:TemplateField>
    
                        <asp:TemplateField HeaderText="Rate">
                            <EditItemTemplate>
                                <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("Rate") %>'></asp:TextBox>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="L2" runat="server" Text='<%# Bind("Rate") %>'></asp:Label>
                            </ItemTemplate>
                            <ItemStyle ForeColor="Black" HorizontalAlign="Right" Width="100px" Wrap="True" />
                        </asp:TemplateField>
    
                        <asp:TemplateField HeaderText="Name2">
                            <EditItemTemplate>
                                <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("Name2") %>'></asp:TextBox>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="L3" runat="server" Text='<%# Bind("Name2") %>'></asp:Label>
                            </ItemTemplate>
                            <ItemStyle ForeColor="Black" HorizontalAlign="Right" Width="100px" Wrap="True" />
                        </asp:TemplateField>
    
    
                        <asp:TemplateField HeaderText="Name2">                    
                            <ItemTemplate>                         
                            </ItemTemplate>
                            <ItemStyle ForeColor="Black" HorizontalAlign="Right" Width="100px" Wrap="True" />
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
    
    
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                Label lb = new Label();
                foreach (GridViewRow row in GridView1.Rows)
                {
                    string a = row.Cells[1].Text;
                    
                    Label l3 = (Label)row.FindControl("L3");
                    string b = l3.Text;
    
                    if (a == b)
                    {
                        lb.Text = "Yes matched";
                    }
                    else
                    {
                        lb.Text = "No matched";
                    }
                    row.Cells[5].Controls.Add(lb);
                }
            }

    The result:

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 8, 2019 9:33 AM