Hi,
am working on a web-application with a database behind it. I would like to be able to populate the rows of a gridview at runtime, means based on a certain condition I need to decide, if a particular Row from the database should be displayed or not.
I tried selectively adding the rows to DataTable first, and then binding the DataTable to the GridView, but didn't work. The Code is below..
/*
StudentTableAdapter adapter = new StudentTableAdapter();
DataTable table = new DataTable();
foreach (DataRow row in adapter.GetStudentMarks())
{
String Branch = row["ProjectName"].ToString();
if (Branch.Equals("Computer-Science"))
{
table.Rows.Add(row);
}
}
GridView1.DataSource = table;
GridView1.DataBind();
*/
Thanks in advance.
Loading
Amith RaiPosted Mar 11, 2011, 6:57 AM
Indeed Mahesh Chand's article about copying row from one table to another explains that, but not completely.
In that article, two methods, he has explained.
1] adding Rows using the method, DataTable.Rows.Add(DataTable Row)
2] adding Rows using the method, DataTable.ImportRow(DataTable Row)
But, the problem with that is, the above two method doesn't rises any exception if the destination table's schema is not defined or if the destination table's schema is different.
So, result is, the solution builds properly without any error, but the rows won't get added to the destination table. Below, I'll post the working code and proper approach.
/*
StudentTableAdapter adapter = new StudentTableAdapter();
DataTable SourceTable = new DataTable();
//GetStudentInfo() is the method in the StudentTable adapter, that is present in the Student dataset. And it fetches the Student information of all the branches.
SourceTable = adapter.GetStudentInfo();
// Student is the dataset name and StudentInfo is the database table name
Student.StudentInfoDataTable DestinationTable = new Student.StudentInfoDataTable();
foreach(DataRow row in table.Rows)
{
String BranchName = row["BranchName"].ToString();
if (ProjectName.Equals("Computer-Science"))
{
DestinationTable.ImportRow(row);
}
}
GridView1.DataSource = newTable;
GridView1.DataBind();
*/
Thanks again.
Amith RaiPosted Mar 11, 2011, 1:51 AM
System.ArgumentException : {"This row already belongs to another table."}