Hi,
Am working on csharp application.In a gridview i want to bind gridview row at a time using for loop.
While binding gridview cell of particular row getting problem.After binding also its showing blank cell.Please help me.
Thanks,
Soumya
Loading
Shaik AliPosted Feb 22, 2010, 1:13 PM
Go thru the following code, it may help.
I am binding each row with an object from a list. I have created a datagrid view with 3 columns.
The complete code is below.
private void BindList()
{
List<TestClass> lstTemp= CreateList(); //create list of objects
for (int i = 0; i < lstTemp.Count; i++)
{
dataGridView1.Rows.Add(); //adding a new row everytime
dataGridView1.Rows[i].Cells["Sno"].Value = lstTemp[i].sNo; //Column name is "Sno"
dataGridView1.Rows[i].Cells["GridName"].Value = lstTemp[i].sName; //Column name is "GridName"
dataGridView1.Rows[i].Cells["PhoneNo"].Value = lstTemp[i].phoneNo; //Column name is "PhoneNo"
}
}
private List<TestClass> CreateList()
{
List<TestClass> lstTest = new List<TestClass>();
lstTest.Add(new TestClass("9","Nine","999999"));
lstTest.Add(new TestClass("8", "Eight", "888888"));
lstTest.Add(new TestClass("7", "Seven", "77777"));
return lstTest;
}
my class structure is :
class TestClass
{
public string sNo { get; set; }
public string sName { get; set; }
public string phoneNo { get; set; }
public TestClass(string _sNo, string _sName, string _phone)
{
sNo = _sNo;
sName = _sName;
phoneNo = _phone;
}
}
Do not forget to Mark as answerd if it solves ur problem
Gaurish KumarPosted Sep 30, 2009, 12:48 AM
Can you define your problem in detail.
Nir BarPosted Sep 23, 2009, 9:21 AM
You can't bind a row at a time. You bind the entire gridview, by using gridview.DataSource property
After you've bound the DataSource, you can retrieve (get, not set) the item that has been bound to a single row by using gridviewrow.DataItem
Please check "Accepted Answer" if this post helps you
Nir