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;
GridView1.DataBind();
Also you can take a Table where you can store value of a Array into one column
as per your requirement.
like;

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;


http://www.codeproject.com/KB/aspnet/datagridview.aspx


http://www.velocityreviews.com/forums/t106436-binding-multidimensional-array-to-datagrid.html


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
    }).ToList();
MyGridView.DataSource = MyList;
MyGridView.DataBind();

Now you can name your DataField property

<asp:GridView ID="MyGridView" runat="server" AutoGenerateColumns="false">
    <Columns>
      <asp:BoundField DataField="MyValue" />
    </Columns>
</asp:GridView>
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);
  conn.Open();
  IEnumerable<string> Elst = null;
  Elst = (IEnumerable<string>)comm.ExecuteReader();    
  Elst.ToArray();
  conn.Close();
}