Click here to Skip to main content
15,890,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,

C#
StreamReader sr = new StreamReader("mydb.txt");
            while ((line = sr.ReadLine()) != "")
            {
                split = line.Split('\t');
                lst.Add(new info { Name = split[0].ToString(), Address = split[1].ToString() });
            }
var btncol = new DataGridViewButtonColumn();
            {
                btncol.HeaderText = "";
                //btncol.Text = lst.Select(c => c.Name);????????
                btncol.UseColumnTextForButtonValue = true;
                btncol.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
                btncol.FlatStyle = FlatStyle.Flat;
                btncol.CellTemplate.Style.BackColor = Color.Honeydew;
                btncol.DisplayIndex =3;
            }

dgv1.Columns.Add(btncol);

in above code i read data from a txt file and put them in a list.
now in my dgv i want to create a data grid view button column which btncol.Text, get data just from Name in list(lst)
what can i do?
Posted
Comments
Mayur Panchal 21-May-13 3:21am    
Is there any other columns present in that Datagrid view control ?
Sunasara Imdadhusen 21-May-13 3:24am    
What is your problem???
mit62 21-May-13 3:32am    
yes there is other buttonCulomns.
this line has error //btncol.Text = lst.Select(c => c.Name);????????
Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<string>' to 'string'

1 solution

int i = 0;
lst.Foreach( s => {
var btncol = new DataGridViewButtonColumn();
{
btncol.HeaderText = "";
btncol.Text = lst[i].Select(c => c.Name).FirstOrDefault();
btncol.UseColumnTextForButtonValue = true;
btncol.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
btncol.FlatStyle = FlatStyle.Flat;
btncol.CellTemplate.Style.BackColor = Color.Honeydew;
btncol.DisplayIndex =3;
}

dgv1.Columns.Add(btncol);
i++;
});
 
Share this answer
 
v3
Comments
mit62 21-May-13 5:56am    
thanks Mohammad but it has this error:

"'openweb.info' does not contain a definition for 'Select' and no extension method 'Select' accepting a first argument of type 'openweb.info' could be found"

"openweb" is my namespace and in my sample code info is a class:

class info
{
private string _name;
public string Name { set { _name = value; } get { return _name; } }
private string _adrs;
public string Address { set { _adrs = value; } get { return _adrs; } }

}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900