ASP.NET - how to assign gridview column value into array

Asked By sunil pandey on 21-Feb-12 12:32 AM
hi all

i am selecting custtomerid from table and i want to assign it in array
and i want to compare it from other value
Somesh Yadav replied to sunil pandey on 21-Feb-12 12:44 AM

There are various ways by using which you can bind data or arrays to gridview.

You can do it as follows;

Person[] persarray = new Person[4];
persarray[0] = new Person("100", "addressline100", "delhi", "110048");
persarray[1] = new Person("101", "addressline101", "bombay", "334243");
persarray[2] = new Person("102", "addressline102", "calcutta", "343234");
persarray[3] = new Person("103", "addressline103", "chennai", "638002");
GridView1.DataSource = persarray;
Also you can take a Table where you can store value of a Array into one column
as per your requirement.

GridView1.Rows[cnt].Cells[cellNo].Text =ds.Tables[0].Rows[][0];

Or you can do something like this;

DataTable dt = new DataTable();
dt.Columns.Add( new DataColumn("Column1", typeof(Int32)));
dt.Columns.Add(new DataColumn("Column2", typeof(string)));
DataRow dr ;
dr= dt.NewRow ();
dr["Column1"] = 1;
dr["Column2"] = "test";
dt.Rows.Add (dr);
DataGrid1.DataSource =dt;
DataGrid1.DataBind ();


So go thr this to achieve your target.

For more details just go thr these links;

Best Luck!!!!!!!!!

Anoop S replied to sunil pandey on 21-Feb-12 01:06 AM
One method is to pass it a class with a single, named field. That way, you can give it a name.

public class GridRecord
    public string MyValue { get; set; }

Then convert your string array to a list of the class

string[] MyArray = new string[1];
MyArray[0] = "My Value";
List<GridRecord> MyList = (
    from ar in myArray
    select new GridRecord
      MyValue = ar
MyGridView.DataSource = MyList;

Now you can name your DataField property

<asp:GridView ID="MyGridView" runat="server" AutoGenerateColumns="false">
      <asp:BoundField DataField="MyValue" />
dipa ahuja replied to sunil pandey on 21-Feb-12 02:23 AM
void getData()
  SqlConnection conn = new SqlConnection("connstring");
  SqlCommand comm = new SqlCommand("SELECT name from table1", conn);
  IEnumerable<string> Elst = null;
  Elst = (IEnumerable<string>)comm.ExecuteReader();