i want to display two columns in datagrid view . first by sql-table second is unbounded where i want some selection button which tells which row in selected.
i get the first column from table but i am not able to figure it out how to add second column.Following is my code.
namespace WindowsFormsApplication14
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string constring = @"server=.\SQLSER;database=test1;integrated securi开发者_如何学Goty=true;";
string sql = @"select rel.depar from rel RIGHT OUTER JOIN cust on cust.id=rel.id";
SqlConnection con = new SqlConnection(constring);
con.Open();
SqlCommand cmd = new SqlCommand(sql,con);
SqlDataReader red = cmd.ExecuteReader();
dataGridView1.ColumnCount = 2;
dataGridView1.Columns[0].Name = "department";
dataGridView1.Columns[1].Name = "unboundcolumn";
while (red.Read())
{
dataGridView1.Rows.Add(red["depar"]);
}
red.Close();
con.Close();
}
}
}
dataGridView1.Columns.Add("myColumn", "My Column");
Update after comment:
You can add the text in the button as a second parameter to add:
dataGridView1.Rows.Add(red["depar"], "Button Text");
精彩评论