Why String.Length is returned instead of the actual value in the DataSource

I have this code that creates IList<string> :

 IList<string> databases; using (MySqlConnection _conn = Session.Connection) using (MySqlCommand _cmd = _conn.CreateCommand("SHOW databases")) { _cmd.Connection.Open ( ); var _dr = _cmd.ExecuteReader(); databases = new List<string> ( _dr.SelectFromReader ( reader => reader[ 0 ] is DBNull ? null : reader[ 0 ].ToString ( ) ) ); _cmd.Connection.Close ( ); } dgrid_Main.DataSource = databases; 

Follow the development of the SelectFromReader extension SelectFromReader here .

The question is how dgrid_Main displays the length of each database ...
display length of database names
... not a name? I checked this test:

 foreach (string db in databases) { // winform treeview control trv_ServerObjects.Nodes.Add ( db ); } 

... I get the following result:
display database name

+4
source share
2 answers

Since the DataSource property on dgrid_Main will bind each public property of the object in the collection, and not the object, it is itself. And Length is one public property of a string object.

When you actually iterate over IList, you get a list of database names.

Try it.

 dgrid_Main.DataSource = databases.ToList().Select(db => new { db }); 
+3
source

I don’t know why this is happening, but tried to use a binding source?

 BindingSource binding1 = new List<string> ( _dr.SelectFromReader ( reader => reader[ 0 ] is DBNull ? null : reader[ 0 ].ToString ( ) ) ); 

and then

 dgrid_Main.DataSource = binding1 
+1
source

All Articles