Showing posts with label GridView. Show all posts
Showing posts with label GridView. Show all posts

Wednesday, 30 November 2016

split , separated string and bind in gridview in asp.net c#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Net.NetworkInformation;
using System.Data.SqlClient;
using System.Data;
using System.Text;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.Runtime.InteropServices;
using System.Management;
using System.Security.Cryptography;

public partial class test : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {

            generatedatatable();
           
        }
    }

    public void generatedatatable()
    {
        try
        {
                  method 1: separate comma and pipe ("," and "|") attached string
            ArrayList List = new ArrayList();             
            //string TstStr = "1,dt3435,26/5/2012 ~ 2,dt738,27/5/2012 ~ 3,dt892,26/5/2012";
            string query = "Select Specification From Menu_new_date where S_no='54'";
            string TstStr = Convert.ToString(connection.scalar(query));
            string[] SplittedString = TstStr.Split('|');
            foreach (string str in SplittedString)
            {
                string ConcatStr = "";
                string[] SubSplit = str.Trim().Split(',');
                foreach (string s in SubSplit)
                {
                    ConcatStr = s " ";
                }
                List.Add(ConcatStr);
            }

            DataTable dt = new DataTable();
            DataColumn dc = new DataColumn("Nutrition", typeof(string));
            //DataColumn dc1 = new DataColumn("Quantity", typeof(string));      
            dt.Columns.Add(dc);
            //dt.Columns.Add(dc1);
            for (int i = 0; i < List.Count; i )
            {
                dt.Rows.Add();
                dt.Rows[i][0] = List[i];
            }
            ------------------------------------------------------------------------------------------

                Method:2  separate comma  (",") attached string in one row with dynamically crate no of column as per the comma
               
            ArrayList List = new ArrayList();
            //string TstStr = "1,dt3435,26/5/2012 ~ 2,dt738,27/5/2012 ~ 3,dt892,26/5/2012";
            string query = "Calories 212 Kcal,Total fat 5.7 g,Saturated fat 0.8 g,Polyunsaturated fats 1 g,Monounsaturated fats 3.4 g,Total                 carbohydrate 25.2 g,Dietary fibre 4.3 g,Sugars 2.3 g,Protein 17.4 g";
            //string TstStr = Convert.ToString(connection.scalar(query));
            string TstStr = query;
            string abc="";
            string[] SplittedString = TstStr.Split(',');
            foreach (string str in SplittedString)
            {
                 abc =str;
                 List.Add(abc);
            }

            DataTable dt = new DataTable();
            //DataColumn dc = new DataColumn("Nutrition", typeof(string));
            //DataColumn dc1 = new DataColumn("Quantity", typeof(string));      
           
            //dt.Columns.Add(dc1);
            dt.Rows.Add();
            for (int i = 0; i < List.Count; i )
            {
                DataColumn dc = new DataColumn("Nutrition" i, typeof(string));
                dt.Columns.Add(dc);
               
               
                dt.Rows[0][i]= List[i];
              
             
            }

            GridView1.DataSource = dt;
            GridView1.DataBind();
            ------------------------------------------------------------------------------------------------

                Method:3  separate comma  (",") attached string in multiple row

            ArrayList List = new ArrayList();
            string TstStr = "1,dt3435,26/5/2012 ~ 2,dt738,27/5/2012 ~ 3,dt892,26/5/2012";
            string query = "Calories 212 Kcal,Total fat 5.7 g,Saturated fat 0.8 g,Polyunsaturated fats 1 g,Monounsaturated fats 3.4 g,Total                 carbohydrate 25.2 g,Dietary fibre 4.3 g,Sugars 2.3 g,Protein 17.4 g";
            string TstStr = Convert.ToString(connection.scalar(query));
            string TstStr = query;
            string abc = "";
            string[] SplittedString = TstStr.Split(',');
            foreach (string str in SplittedString)
            {
                abc = str;
                List.Add(abc);
            }

            DataTable dt = new DataTable();
            DataColumn dc = new DataColumn("Nutrition", typeof(string));
            DataColumn dc1 = new DataColumn("Quantity", typeof(string));      

          
            dt.Columns.Add(dc);
            for (int i = 0; i < List.Count; i )
            {                             
                dt.Rows.Add();
                dt.Rows[i][0] = List[i];
            }
                //create gridview dynamically and add this gridview to any div where to show data
            GridView grid = new GridView();
            grid.DataSource = dt;
            grid.DataBind();
            nutri.Controls.Add(grid);
               
        //bind splitted string to available gridview
            GridView1.DataSource = dt;
            GridView1.DataBind();

                        
Nutrition
Calories 212 Kcal
Total fat 5.7 g
Saturated fat 0.8 g
Polyunsaturated fats 1 g
Monounsaturated fats 3.4 g
Total carbohydrate 25.2 g
Dietary fibre 4.3 g
Sugars 2.3 g
Protein 17.4 g


            --------------------------------------------------------------------------------------------------------
         Method:4  separate  comma and pipe ("," and "|") attached string in multiple row
            DataTable values = new DataTable();
            values.Columns.Add("Nutrition", typeof(string), null);
            values.Columns.Add("Quantity", typeof(string), null);

            string query = "Select Specification From Menu_new_date where S_no='54'";
            string TstStr = Convert.ToString(connection.scalar(query));
            values = TstStr.Split('|').Select(s =>
            {
                var fields = s.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                var row = values.NewRow();
                if (fields.Length == 2)
                {
                    row[0] = fields[0].Trim();
                    row[1] = fields[1].Trim();                  
                }
                return row;
            }).CopyToDataTable();

            GridView1.DataSource = values;
            GridView1.DataBind();
        }
        catch (Exception ex)
        {
            Label1.Text= ex.Message;
        }
    }
   
}

How to seach in gridview using key up method like table in html


// Add a gridview to the aspx page and a textbox for search. bind the gridview for populating the data

Put this java script in head of the aspx page

  <script type="text/javascript">
        function filter2(phrase) {
            var words = phrase.value.toLowerCase().split(" ");
            var table = document.getElementById("<%=GridView1.ClientID %>");
            var ele;
            for (var r = 1; r < table.rows.length; r ) {
                ele = table.rows[r].innerHTML.replace(/<[^>] >/g, "");
                var displayStyle = 'none';
                for (var i = 0; i < words.length; i ) {
                    if (ele.toLowerCase().indexOf(words[i]) >= 0)
                        displayStyle = '';
                    else {
                        displayStyle = 'none';
                        break;
                    }
                }
                table.rows[r].style.display = displayStyle;
            }
        }
    </script>

Now add this function "keyup" in the texbox which will be used to enter the keyword like this

 <asp:TextBox ID="TextBox2" runat="server" class="form-control" onkeyup="filter2(this)"></asp:TextBox>