怎样把数据库表中一列的数据用listbox显示
public void ListBox1()
{
DataTable dt;
String[] uName = new String[dt.Rows.Count];
String[] uId = new String[dt.Rows.Count];
for (int i = 0; i < dt.Rows.Count;i++ )
{
uName[i] = dt.Rows[i]["name"].ToString();
uId[i] = dt.Rows[i]["id"].ToString();
}
listBox1.Items.AddRange(uName);
}
private void listBox_SelectedIndexChanged(object sender, EventArgs e)
{
txtUserName.Text =uId(listBox1.SelectedIndex);
}
怎么邦定呀?本人刚入门学习C#,请各位高手多多指教。 展开
也没有看到你的command
给dt绑数据:
DataTable dt;
dt.DataSet = ....;
或者
用一个DataAdapter执行语句后填充dt;
示例代码:
这个方法获得数据源,返回一个DataTable
public DataTable Select(string employeeId, string date)
{
const string IDENTITY = "IF_DailyReport_L.Select(string employeeId, string date)";
string strConnection = DBAccess.GetSqlConnectionString();
SqlConnection cn = new SqlConnection(strConnection);
StringBuilder sb = new StringBuilder();
try
{
LogHelper.SrvLogger.InfoFormat(LogResource.InfoInvokeBegin, IDENTITY);
sb.Append(" SELECT ");
sb.Append("DAILYREPORTID,");
sb.Append("EMPLOYEEID,");
sb.Append("(SELECT T_EMPLOYEE.EMPLOYEE_NAME FROM T_EMPLOYEE WHERE T_EMPLOYEE.EMPLOYEEID=T_DAILYREPORT.EMPLOYEEID) EMPLOYEE_NAME,");
sb.Append("LEADERID,");
sb.Append("(SELECT T_EMPLOYEE.EMPLOYEE_NAME FROM T_EMPLOYEE WHERE T_EMPLOYEE.EMPLOYEEID=T_DAILYREPORT.LEADERID) LEADER_NAME,");
sb.Append("Date,");
sb.Append("MORNING_JOB_CONTENT,");
sb.Append("AFTERNOON_JOB_CONTENT,");
sb.Append("APPROVE_RESPONSE,");
sb.Append("STATUS,");
sb.Append("(SELECT NAME FROM T_SYSTEM_CODE WHERE CODE=38 AND ID=STATUS) STATUS_NAME");
sb.Append(" FROM T_DAILYREPORT WHERE 1=1 ");
if (employeeId.Trim().Length > 0 && employeeId != "0")
sb.Append(" AND EMPLOYEEID=@employeeId");
if (date.Trim().Length > 0)
sb.Append(" AND Date=@date");
sb.Append(" ORDER BY Date DESC");
SqlCommand cm = new SqlCommand(sb.ToString(), cn);
if (employeeId.Trim().Length > 0 && employeeId != "0")
cm.Parameters.Add("@employeeId", SqlDbType.VarChar, 4).Value = employeeId.Trim();
if (date.Trim().Length > 0)
cm.Parameters.Add("@date", SqlDbType.DateTime).Value = Convert.ToDateTime(date);
string sql = sb.ToString();
LogHelper.SrvLogger.DebugFormat(LogResource.DebugExecSQL, IDENTITY, sql, employeeId + ";" + date.Trim());
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cm);
da.Fill(dt);
LogHelper.SrvLogger.InfoFormat(LogResource.InfoReturnCount, IDENTITY, (dt != null && dt.Rows != null) ? dt.Rows.Count : 0);
return dt;
}
catch (Exception ex)
{
LogHelper.SrvLogger.ErrorFormat(LogResource.ErrorError, ex, employeeId + ";" + date);
throw (ex);
}
finally
{
cn.Close();
LogHelper.SrvLogger.InfoFormat(LogResource.InfoInvokeEnd, IDENTITY);
}
}
在你的DataTable dt;下面添加
dt=Select(employeeId, date);
即能把数据源绑上.
移动到右边 是 listbox2.Items.Add(listbox1.Items[listbox1.itemindex]);
var
i:integer;
begin
for i:=1 to listbox2.Count then
begin
with adoquery1 do
begin
close;
sql.Clear;
sql.Add('insert 表(字段名) values('''+listbox2.Items[i]+''' )');
execsql;
end;
end;
try
{
dt = new DataTable("biao");
DataColumn dc1 = dt.Columns.Add("number", typeof(string));
dt.PrimaryKey = new DataColumn[] { dc1 };
dc1 = dt.Columns.Add("name", typeof(string));
dt.Rows.Add("one", "tom");
dt.Rows.Add("two", "tom");
dt.Rows.Add("three", "tom");
dt.Rows.Add("four", "tom");
dt.Rows.Add("five", "tom");
listBox1.DataSource = dt;
listBox1.DisplayMember = "number";
listBox1.ValueMember = "number";
}