Click here to Skip to main content
15,892,643 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have a gridview as below: OPEN


Data is binded from the database which is like : OPEN

I want something like: OPEN


Help would really be appreciated, Thank You.

What I have tried:

I have tried thinking a lot about it, but my logic is not working as I am a newbie.
Posted
Updated 11-Jul-18 5:02am
v2
Comments
CHill60 11-Jul-18 9:00am    
The "What I have tried:" section is for you to put your code. We can then help you work out what is wrong. For example I would need to see "how" you have bound the data to the gridview - what query are you using. What database is it? (It looks like SQL Server but I can't assume that). If it is SQL Server what version is it (as this will impact how you can make a comma separated list from the rows)
Shashank Laxman 12-Jul-18 4:18am    
Just for simplicity purpose i have used List and then iterated.
You will obviously use DataReader instead of list.

Just paste the code which i have posted and run.

You will have to make changes in key check -
if (MyDictionary.ContainsKey("admin"))

Remove hardcoding part.

1 solution

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebInterface.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click"/>

        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
    </div>
    </form>
</body>
</html>




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

namespace WebInterface
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //DropDownList1.Items.Add("MEDICAL");
            //TextBox1.Text = "1Jan2000";
            //TextBox2.Text = "2Jan2000";
            //TextBox3.Text = "FEVER";

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            ConcurrentDictionary<string, string> MyDictionary = new ConcurrentDictionary<string, string>();
            Company l = new Company();

            List<string> lstComp = new List<string>();

            lstComp.Add("admin-Krishna Softwares");
            lstComp.Add("admin-abc");
            lstComp.Add("ks1-Krishna Softwares");
            lstComp.Add("ks1-Krishna Softwares");


            foreach (var t in lstComp)
            {
                String[] y = t.Split('-');

                string Company = "";
                StringBuilder sb = new StringBuilder();

                if (MyDictionary.ContainsKey("admin"))
                {
                    if (MyDictionary.TryGetValue("admin", out Company))
                    {
                        sb.Append(Company + "," + y[1]);
                        MyDictionary.TryAdd(y[0], sb.ToString());
                    }
                }
                else if (MyDictionary.ContainsKey("ks1"))
                {
                    if (MyDictionary.TryGetValue("ks1", out Company))
                    {
                        sb.Append(Company + "," + y[1]);
                        MyDictionary.TryAdd(y[0], sb.ToString());
                    }
                }
                else
                    MyDictionary.TryAdd(y[0], y[1]);
            }
            GridView1.DataSource = MyDictionary.Select(m => new { m.Key,m.Value});
            GridView1.DataBind();



            //l.LeaveType = DropDownList1.SelectedValue.Trim();
            //l.FromDate = TextBox1.Text.Trim();
            //l.ToDate = TextBox2.Text.Trim();
            //l.Reason = TextBox3.Text.Trim();
            //MyDictionary.TryAdd(l.FromDate, l);
            //GridView1.DataSource = MyDictionary.Select(m => new { m.Value.LeaveType, m.Value.FromDate, m.Value.ToDate, m.Value.Reason });
            //GridView1.DataBind();



        }

        public class Leave
        {
            public string LeaveType;
            public string FromDate;
            public string ToDate;
            public string Reason;
        }

        public class Company
        {
            public string userid;
            public string compname;
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900