Reading datagridview

I populated a datagridview from a datatable. How do I read from the datagridview when the application is running?

Answers


how did you populate it? is the DataSource something useful like a BindlingList? If it is then something like:

BindingSource bindingSource = this.dataGridView1.DataSource as BindingSource;
//substitute your business object type for T 
T entity = bindingSource.Current as T;

would get you the entity bound to the row.

Otherwise there is always the datagridview.Columns[n].Cells[n].Value but really I'd look at using the objects in the DataSource

Edit: Ah... a datatable... righto:

 var table = dataGridView1.DataSource as DataTable;

 foreach(DataRow row in table.Rows)
 {
     foreach(DataColumn column in table.Columns)
     {
         Console.WriteLine(row[column]);
     }
 }

You can iterate through your datagridview and retrieve each cell.

for(int i =0; i < DataGridView.Rows.Count; i++){
  DataGridView.Rows.Columns["columnName"].Text= "";
}

There is an example here.


Need Your Help

Java <Integer, String, String, String> collection and operations on data from txt file

java string collections integer

I've used information from this link, but I have a lot of problems with other things.

django for cycle add variables together

django templates for-loop addition

I am facing a problem writing in simple for cycle in template. This is what I am trying to write in pseudo-code.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.