Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9282931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T18:30:06+00:00 2026-06-18T18:30:06+00:00

I want to create a GridView Control in Code Behind. Data is fetched from

  • 0

I want to create a GridView Control in Code Behind.

Data is fetched from database table.

My code is:

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

public partial class gv1 : System.Web.UI.Page
{
    SqlConnection cn;
   SqlCommand cmd;
   DataSet ds;
   SqlDataAdapter da;

   protected void Page_Load(object sender, EventArgs e)
  {
       cn = new SqlConnection("Data Source=AMIR-PC\\MOHEMMAD;Initial Catalog=CRM_InvestPlus;Integrated Security=True");

}
protected void Button1_Click(object sender, EventArgs e)
{
    cn.Open();
    cmd = new SqlCommand("Select * from Customer_Master", cn);
    da = new SqlDataAdapter(cmd);
    ds = new DataSet();
    da.Fill(ds);
    cn.Close();

    GridView gr1 = new GridView();

    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
    {
        GridViewRow row = new GridViewRow(i, i, DataControlRowType.DataRow,  DataControlRowState.Normal);
        TableCell cell1 = new TableCell();
        TableCell cell2 = new TableCell();
        cell1.Text = ds.Tables[0].Rows[i][0].ToString();
        cell2.Text = ds.Tables[0].Rows[i][0].ToString();
        row.Cells.Add(cell1);
        row.Cells.Add(cell2);
        gr1.Controls[0].Controls.AddAt(i, row);


      }
        this.Controls.Add(gr1);
   }
}

But when I Run the code error occurs as follows:

ArgumentOutOfRangeException was unhandled by user at the line of code showing

gr1.Controls[0].Controls.AddAt(i,row);

Please help..
Thanks in advance..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T18:30:07+00:00Added an answer on June 18, 2026 at 6:30 pm

    I think this occurs because you are adding to Control[0] that is not set anywhere. Change it like this and see if it helps:

    gr1.Controls.AddAt(i, row);

    Ok this is how it should like:

    First when you are initializing your grid view you have to add to it Table

    var gv = new GridView();
    var t = new Table();
    gv.Controls.Add(t);
    

    Then you can add you row the way you were doing

    gv.Controls[0].AddAt(i, row);
    

    Please try this way 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a gridview that shows records from a database table. Now, I want
i want create a custom json data from the mssql 2008 results so that
I'm using asp.nets webforms and gridview to create large data tables on my website.
I have gridview bind in user control and want to access that gridview from
I have a gridview that i want to fill with data from my linq
I have created gridview in code behind file of C#. I want to place
I want to create a full screen GridView control for a Metro Style application
i want create multiple search where statement $where_search is a multiple condition from post
i want create Dynamic Slideshow in Jquery i'm Write this code var ctx =
in my application im exporting gridview data to excel sheet now i want save

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.