C#一些经常忘的小东西

来源:互联网 发布:矩阵分析 答案pdf 编辑:程序博客网 时间:2024/05/29 18:37

1 窗体中的 comboBox


comboBox1.DataSource = dt_com;

comboBox1.DisplayMember = "Buyer";

comboBox1.ValueMember = "Id";


2给dataGridView赋值,其中有自定义的列

dt_umd = tu.SelectAll("order by id");
DataTable dt = new DataTable();//新建的表

dt.Columns.Add("id", Type.GetType("System.String"));
dt.Columns.Add("address", Type.GetType("System.String"));
dt.Columns.Add("com", Type.GetType("System.String"));
dt.Columns.Add("version", Type.GetType("System.String"));
dt.Columns.Add("yuanming", Type.GetType("System.String"));

 for (int i = 0; i < dt_umd.Rows.Count; i++)
 {
                DataRow dr = dt.NewRow();
                dr["id"] = dt_umd.Rows[i]["id"];
                dr["address"] = dt_umd.Rows[i]["address"];
                dr["com"] = dt_umd.Rows[i]["com"];
                dr["version"] = dt_umd.Rows[i]["version"];


                dt_sn = ts.SelectAll("and id=" + dt_umd.Rows[i]["sId"].ToString() + "");
                if (dt_sn.Rows.Count > 0)
                {
                    dr["yuanming"] = dt_sn.Rows[0]["name"].ToString();
                }
                else
                {
                    dr["yuanming"] = "";
                }
                dt.Rows.Add(dr);
 }

 dataGridView1.DataSource = dt.DefaultView;


3 dataGridView 右键选择行中的哪列值

string name = "";
 DataGridViewSelectedRowCollection dgvsrow = dataGridView1.SelectedRows;
 foreach (DataGridViewRow dgvr in dgvsrow)
 {
         name = dgvr.Cells["id"].Value.ToString();
 }