C# 一个datagridview绑定sql数据库的多张表(用datagridview显示数据库连表查询语句的结果)?
展开全部
string Str = "mealid|套餐编号;mealname|套餐名称;mealprice|套餐价格;unit|单位;mealdemo|套餐说明"; string TableName = "T_Meal"; makeSql.SetControlData(dataGridView2, Str, TableName, null, null); Str = "bookId|预约编号;memphone|手机号码;mealid|套餐编号;mealname|套餐名称;mealprice|套餐价格;mealcount|套餐数量;unit|单位;booktime|预定时间;bookstarttime|开餐时间;ischeckbookdetailout|支付状态"; TableName = "V_Ordermeal"; string WhereSql = " and ischeckbookdetailout='未支付' and memId='"+label2 .Text +"'"; makeSql.SetControlData(dataGridView1, Str, TableName, WhereSql, null);
//增加datagridview的列名 public void SetColumnName(DataGridView dataGridView, int width, string name) { //dataGridView.Columns[name].HeaderText = cName; dataGridView.Columns[name].Width = width; }
//根据树节点或者下拉框的数据改变来动态生成Datagridview的列 public void SetControlData(DataGridView myGrid, string Str, string TableName, string WhereSql, string StateChange) {
System.Data.DataTable table = new DataTable(); System.Data.DataColumn column = new DataColumn();
DataGridViewCheckBoxColumn myCol = new DataGridViewCheckBoxColumn();
myCol.TrueValue = 1; myCol.FalseValue = 0; myCol.HeaderText = "选择";
myGrid.Columns.Insert(0, myCol);
// myGrid.Columns.Add(myCol);
string dic = DoubleDicForSql(Str);
string sql = string.Format("select" + dic + " from " + TableName + " where 1=1 "); sql = sql + WhereSql; DataTable dt = null; dt = SqlHelper.SqlHelperService.ExecuteQuery(sql); myGrid.DataSource = dt;
table.Merge(dt); myGrid.DataSource = table; myGrid.Columns[0].Width = 40; if (!string.IsNullOrEmpty(StateChange)) { string[] State = StateChange.Split(';'); for (int i = 0; i < State.Length; i++) { string name = State[i].Split('|')[0]; string count = State[i].Split('|')[1]; string rename = State[i].Split('|')[2]; for (int j = 0; j < myGrid.RowCount; j++) { string discount = myGrid.Rows[j].Cells[name].Value.ToString().Trim(); if (discount == count) { myGrid.Rows[j].Cells[name].Value = rename; }
} } } for (int i = 1; i < myGrid.ColumnCount; i++) { myGrid.Columns[i].ReadOnly = true; } myGrid.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
} }
//增加datagridview的列名 public void SetColumnName(DataGridView dataGridView, int width, string name) { //dataGridView.Columns[name].HeaderText = cName; dataGridView.Columns[name].Width = width; }
//根据树节点或者下拉框的数据改变来动态生成Datagridview的列 public void SetControlData(DataGridView myGrid, string Str, string TableName, string WhereSql, string StateChange) {
System.Data.DataTable table = new DataTable(); System.Data.DataColumn column = new DataColumn();
DataGridViewCheckBoxColumn myCol = new DataGridViewCheckBoxColumn();
myCol.TrueValue = 1; myCol.FalseValue = 0; myCol.HeaderText = "选择";
myGrid.Columns.Insert(0, myCol);
// myGrid.Columns.Add(myCol);
string dic = DoubleDicForSql(Str);
string sql = string.Format("select" + dic + " from " + TableName + " where 1=1 "); sql = sql + WhereSql; DataTable dt = null; dt = SqlHelper.SqlHelperService.ExecuteQuery(sql); myGrid.DataSource = dt;
table.Merge(dt); myGrid.DataSource = table; myGrid.Columns[0].Width = 40; if (!string.IsNullOrEmpty(StateChange)) { string[] State = StateChange.Split(';'); for (int i = 0; i < State.Length; i++) { string name = State[i].Split('|')[0]; string count = State[i].Split('|')[1]; string rename = State[i].Split('|')[2]; for (int j = 0; j < myGrid.RowCount; j++) { string discount = myGrid.Rows[j].Cells[name].Value.ToString().Trim(); if (discount == count) { myGrid.Rows[j].Cells[name].Value = rename; }
} } } for (int i = 1; i < myGrid.ColumnCount; i++) { myGrid.Columns[i].ReadOnly = true; } myGrid.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
} }
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询