0
protected void Button1_Click(object sender, EventArgs e)
{
    string sqlstring;
    OleDbConnection connection = new OleDbConnection("Provider=MSDAORA;Data Source=solaris;Password=medical;User ID=medical");
    sqlstring = "select emp_name,decode(sex,null,null,'Self')rel,decode(dob,null,'Ad',floor(months_between(sysdate,dob)/12))age,decode(f_elg,'Y',f_name,null) f_name,decode(f_elg,'Y','Father',null) f_rel,decode(f_dob,null,'Ad',floor(months_between(sysdate,f_dob)/12)) f_age,decode(m_elg,'Y',m_name,null) m_name,decode(m_elg,'Y','Mother',null) m_rel,decode(m_dob,null,'Ad',floor(months_between(sysdate,m_dob)/12)) m_age,decode(s_elg,'Y',s_name,null) s_name,decode(s_elg,'Y',decode(s_sex,'M','Husband','F','Wife'),null) s_rel,decode(s_dob,null,'Ad',floor(months_between(sysdate,s_dob)/12)) s_age,decode(dep_elg1,'Y',dep_name1,null) dep_name1,decode(dep_sex1,'M','Son','F','Daughter',null) dep_rel1,decode(dep_dob1,null,null,floor(months_between(sysdate,dep_dob1)/12)) dep_age1,decode(dep_elg2,'Y',dep_name2,null) dep_name2,decode(dep_sex2,'M','Son','F','Daughter',null) dep_rel2,decode(dep_dob2,null,null,floor(months_between(sysdate,dep_dob2)/12)) dep_age2,decode(dep_elg3,'Y',dep_name3,null) dep_name3,decode(dep_sex3,'M','Son','F','Daughter',null) dep_rel3,decode(dep_dob3,null,null,floor(months_between(sysdate,dep_dob3)/12)) dep_age3,decode(dep_elg4,'Y',dep_name4,null) dep_name4,decode(dep_sex4,'M','Son','F','Daughter',null) dep_rel4,decode(dep_dob4,null,null,floor(months_between(sysdate,dep_dob4)/12)) dep_age4,decode(dep_elg5,'Y',dep_name5,null) dep_name5,decode(dep_sex5,'M','Son','F','Daughter',null) dep_rel5,decode(dep_dob5,null,null,floor(months_between(sysdate,dep_dob5)/12)) dep_age5,decode(dep_elg6,'Y',dep_name6,null) dep_name6,decode(dep_sex6,'M','Son','F','Daughter',null) dep_rel6,decode(dep_dob6,null,null,floor(months_between(sysdate,dep_dob6)/12)) dep_age6 from employee_mas where emp_no='" + TextBox1.Text + "'";
    //sqlstring = "select COALESCE(emp_name,'') + ' ' + COALESCE(sex,'')as empinfo from employee_mas where emp_no='" + TextBox1.Text + "'";

    OleDbCommand comm = new OleDbCommand(sqlstring, connection);


    OleDbDataReader reader;


    connection.Open();


    reader = comm.ExecuteReader();
    if (reader.Read())
    {
        ListBox1.Items.Add(reader.GetString(reader.GetOrdinal("emp_name")));
        ListBox1.Items.Add(reader.GetString(reader.GetOrdinal("age")));
        ListBox1.Items.Add(reader.GetString(reader.GetOrdinal("rel")));
        ListBox1.Items.Add(reader.GetString(reader.GetOrdinal("f_name")));
        ListBox1.Items.Add(reader.GetString(reader.GetOrdinal("f_age")));
        ListBox1.Items.Add(reader.GetString(reader.GetOrdinal("f_rel")));


    } 

}       

这是我用来填充列表框的代码

现在我想要我的 gridview 有 3 列,例如:

Name     Age      Rel

所以前 3 个值被插入到 gridview 的第一行,第二个 3 被插入到第二个中,依此类推。

帮助会很大。

4

1 回答 1

0

您可以使用 DataTable 来存储行,然后在读取所有记录后,将其绑定到 GridView:

protected void Button1_Click(object sender, EventArgs e)
{
    string sqlstring;
    OleDbConnection connection = new OleDbConnection("Provider=MSDAORA;Data Source=solaris;Password=medical;User ID=medical");
    sqlstring = "select emp_name,decode(sex,null,null,'Self')rel,decode(dob,null,'Ad',floor(months_between(sysdate,dob)/12))age,decode(f_elg,'Y',f_name,null) f_name,decode(f_elg,'Y','Father',null) f_rel,decode(f_dob,null,'Ad',floor(months_between(sysdate,f_dob)/12)) f_age,decode(m_elg,'Y',m_name,null) m_name,decode(m_elg,'Y','Mother',null) m_rel,decode(m_dob,null,'Ad',floor(months_between(sysdate,m_dob)/12)) m_age,decode(s_elg,'Y',s_name,null) s_name,decode(s_elg,'Y',decode(s_sex,'M','Husband','F','Wife'),null) s_rel,decode(s_dob,null,'Ad',floor(months_between(sysdate,s_dob)/12)) s_age,decode(dep_elg1,'Y',dep_name1,null) dep_name1,decode(dep_sex1,'M','Son','F','Daughter',null) dep_rel1,decode(dep_dob1,null,null,floor(months_between(sysdate,dep_dob1)/12)) dep_age1,decode(dep_elg2,'Y',dep_name2,null) dep_name2,decode(dep_sex2,'M','Son','F','Daughter',null) dep_rel2,decode(dep_dob2,null,null,floor(months_between(sysdate,dep_dob2)/12)) dep_age2,decode(dep_elg3,'Y',dep_name3,null) dep_name3,decode(dep_sex3,'M','Son','F','Daughter',null) dep_rel3,decode(dep_dob3,null,null,floor(months_between(sysdate,dep_dob3)/12)) dep_age3,decode(dep_elg4,'Y',dep_name4,null) dep_name4,decode(dep_sex4,'M','Son','F','Daughter',null) dep_rel4,decode(dep_dob4,null,null,floor(months_between(sysdate,dep_dob4)/12)) dep_age4,decode(dep_elg5,'Y',dep_name5,null) dep_name5,decode(dep_sex5,'M','Son','F','Daughter',null) dep_rel5,decode(dep_dob5,null,null,floor(months_between(sysdate,dep_dob5)/12)) dep_age5,decode(dep_elg6,'Y',dep_name6,null) dep_name6,decode(dep_sex6,'M','Son','F','Daughter',null) dep_rel6,decode(dep_dob6,null,null,floor(months_between(sysdate,dep_dob6)/12)) dep_age6 from employee_mas where emp_no='" + TextBox1.Text + "'";
    //sqlstring = "select COALESCE(emp_name,'') + ' ' + COALESCE(sex,'')as empinfo from employee_mas where emp_no='" + TextBox1.Text + "'";

    OleDbCommand comm = new OleDbCommand(sqlstring, connection);

    OleDbDataReader reader;

    connection.Open();

    DataTable table = new DataTable();
    table.Columns.Add("Name");
    table.Columns.Add("Age");
    table.Columns.Add("Rel");

    reader = comm.ExecuteReader();
    if (reader.Read())
    {
        string name = reader.GetString(reader.GetOrdinal("emp_name"));
        string age = reader.GetString(reader.GetOrdinal("age"));
        string rel = reader.GetString(reader.GetOrdinal("rel"));

        ListBox1.Items.Add(name);
        ListBox1.Items.Add(age);
        ListBox1.Items.Add(rel);
        ListBox1.Items.Add(reader.GetString(reader.GetOrdinal("f_name")));
        ListBox1.Items.Add(reader.GetString(reader.GetOrdinal("f_age")));
        ListBox1.Items.Add(reader.GetString(reader.GetOrdinal("f_rel")));

        table.Rows.Add(name, age, rel);
    }

    GridView.DataSource = table;
    GridView.DataBind();
}   
于 2013-02-09T13:07:53.730 回答