Hi,
I need to fill my datagridview in csharp using datareader, how i do it?
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Priya LingePosted Aug 30, 2011, 2:05 AM
You can fill datagridView using datareder as following way,
public void getdata()
{
ArrayList arrlist = new ArrayList();
DataGridView myDataGrid = new DataGridView();
sqlConnection.Open();
string query = "select * from Customer";
sqlCommand = new SqlCommand(query, sqlConnection);
sqlReader = sqlCommand.ExecuteReader();
while (sqlReader.Read())
{
Customerdetails cus = new Customerdetails();
cus.Id =Convert.ToInt32(sqlReader[0].ToString());
cus.Name = sqlReader[1].ToString();
arrlist.Add(cus);
}
myDataGrid.DataSource = arrlist;
myDataGrid.Show();
panel1.Controls.Add(myDataGrid);
}
Here we have one class name Customerdetails
class Customerdetails
{
private string name;
private int id;
public string Name
{
get { return name; }
set { name = value; }
}
public int Id
{
get { return id; }
set { id = value; }
}
}
Hope this will help you.
Thanks.
Black DiamondPosted Aug 30, 2011, 2:18 AM
Thank you for your code, it works.....
Jiteendra SampathiraoPosted Aug 30, 2011, 2:13 AM
void Page_Load(Object sender, EventArgs e)
{
String Qry;
Sqlcommand cmd;
SqlDataReader dr;
SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["sqlserver"]);
conn.Open();
Qry = "select a,b,c,d from your_table_name";
cmd= New Sqlcommand(Qry, conn);
dr = cmd.ExecuteReader();
Gridview1.DataSource = dr;
Gridview1.DataBind();
}