Bind Gridview In Asp.Net Using Two Dimensional Array

Bind Gridview In Asp.Net Using Two Dimensional Array



Step 1: Design Web Page. 



<form id="form1" runat="server">
    <div>
    <asp:GridView ID="gr" runat="server">

    </asp:GridView>
    </div>
    </form>





Step 2 : Logic on .CS Page.

                protected void Page_Load(object sender, EventArgs e)
    {
        bind_gr();
    }

    public void bind_gr()
    {
        //Array Decleration
        string[,] gr_array = { {"kandy","Delhi"}, {"sam","UP"}, {"Rahul","HR"}, {"Jyotishmoy","ASssm"} };
     
        //Checking Length Of array

        if (gr_array.Length> 0)

        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Name");
            dt.Columns.Add("City");
           
            //Assigning Value to DataTable

            for (int i = 0; i <= gr_array.GetUpperBound(0); i++)
            {
                dt.Rows.Add();
                dt.Rows[i]["name"] = gr_array[i,0].ToString();
                dt.Rows[i]["City"] = gr_array[i,1].ToString();
            }
            gr.DataSource = dt;
            gr.DataBind();
        }

    }

It Will Look Like :





Comments

Popular posts from this blog

Create and save QR code in asp.net with C#

Change text of RadGrid Header Dynamically

Telerik Radwindow Open on Button Click