none
下图中的1-100的小方格是如何实现的? RRS feed

答案

  •         DataTable dt;
            DataRow dr;
            int num=0;
            ArrayList al;
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                al = new ArrayList();
                dt = new DataTable();
                dt.Columns.Add("1");
                dt.Columns.Add("2");
                dt.Columns.Add("3");
                dt.Columns.Add("4");
    
                for (int i = 0; i < 4; i++)
                {
                    dr = dt.NewRow();
                    dr[0] = num + 1;
                    dr[1] = num + 2;
                    dr[2] = num + 3;
                    dr[3] = num + 4;
    
                    num = num + 4;
    
                    dt.Rows.Add(dr);
                      
                }
    
                TextMessage();
    
                dataGridView1.DataSource = dt;
            }
    
            private void TextMessage()
            {
                for (int i = 0; i < 16; i++)
                {
                    al.Add("第" + Convert.ToString(i + 1) + "题");
                }
    
            }
    
            private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
            {
                textBox1.Text = al[Convert.ToInt32(dataGridView1.CurrentCell.Value.ToString()) -1].ToString();
            }

    <embed height="0" hidden="true" id="ciba_grabword_plugin" type="application/ciba-grabword-plugin" width="0" />

    If this is helpful
      { Please Mark as Answered }
    else if it is not helpful
      { Un-Mark as Answered }


    Best Regards
    Huan Li, Code Blog: Small is New Big In C#

    • 已标记为答案 呆oO 2014年8月16日 3:28
    2012年10月14日 12:13
  •         DataSet ds;
            DataTable dt;
            DataRow dr;
    
            int num = 0;
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                //取出所有题库表Table1里的所有题目
                ds = this.GetDataSet("SELECT ID AS 题号, QUESTION AS 问题 FROM Table1", "题库");
    
                //初始化DataGridView选题田字格
                //ds.Tables[0].Rows.Count / 8 的目的表示 一行上显示几个数字,假定一行显示8个题号
                dt = new DataTable();
                for (int i = 0; i < ds.Tables[0].Rows.Count / 8; i++)  
                {
                    dr = dt.NewRow();
                    dr[0] = num + 1;
                    dr[0] = num + 2;
                    dr[0] = num + 3;
                    dr[0] = num + 4;
                    dr[0] = num + 5;
                    dr[0] = num + 6;
                    dr[0] = num + 7;
                    dr[0] = num + 8;
    
                    num = num + 8;
    
                    dt.Rows.Add(dr);
                }
                dataGridView1.DataSource = dt;
            }
    
    
            private void dataGridView1_CellClick_1(object sender, DataGridViewCellEventArgs e)
            {
                DataView dv;
                DataTable newdt;
                int index;
    
                //获取指定题号
                index = Convert.ToInt32(dataGridView1.CurrentCell.Value.ToString()) - 1;
    
                //从dataset里过滤取出题号对应的问题
                dv = ds.Tables[0].DefaultView;
                dv.RowFilter = "题号 = " + index.ToString() + "";
                newdt = dv.ToTable(true, "问题");
    
                //在textbox显示出问题内容
                textBox1.Text = newdt.Rows[0][0].ToString();
            }
    
            //创建DataSet对象
            public DataSet GetDataSet(string sqlComm, string tableName)
            {
                SqlConnection sqlconn = this.GetSqlConn();
                SqlDataAdapter sqlda = new SqlDataAdapter(sqlComm, sqlconn);
                DataSet ds = new DataSet();
                sqlda.Fill(ds, tableName);
                return ds;
            }
    
            //获取数据库连接 读取config.ini配置文件
            public SqlConnection GetSqlConn()
            {
    
                try
                {
                    string strconn = "user id=sa;data source=. persist security info=True;initial catalog=DBName;password=123456";
                    sqlconn = new SqlConnection(strconn);
                    sqlconn.Open();
                    return sqlconn;
                }
                catch (Exception ex)
                {
                    sqlconn = null;
                    return sqlconn;
                }
            }


    If this is helpful
      { Please Mark as Answered }
    else if it is not helpful
      { Un-Mark as Answered }


    Best Regards
    Huan Li, Code Blog: Small is New Big In C#

    • 已标记为答案 呆oO 2014年8月16日 3:29
    2012年10月15日 2:36

  • <embed height="0" hidden="true" id="ciba_grabword_plugin" type="application/ciba-grabword-plugin" width="0" />dt1 = new datatable()这句不对

    应该是dt1= dt.Table[0]


    If this is helpful
      { Please Mark as Answered }
    else if it is not helpful
      { Un-Mark as Answered }


    Best Regards
    Huan Li, Code Blog: Small is New Big In C#

    • 已标记为答案 呆oO 2014年8月16日 3:29
    2012年11月5日 8:16

全部回复

  • Hi 呆oO ,

      欢迎来到MSDN Forum 中文论坛。 你数据显示可以使用DataGridView 控件,你可以用DataSource制定数据源,DataSet指定数据集,或者用linq to entities, linq to sql 或者linq to object来查询得到数据集合来显示。

     


    Jason Wang [MSFT]
    MSDN Community Support | Feedback to us

    2012年10月3日 2:14
    版主
  • 你可以使用 DataGridView 与资料库连接。
    2012年10月3日 4:05
  • 求个示例,谢谢


    C#初学者!~~~

    2012年10月11日 5:17
  • 难道就真的没有高人给个示例吗?


    C#初学者!~~~

    2012年10月14日 7:35
  •         DataTable dt;
            DataRow dr;
            int num=0;
            ArrayList al;
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                al = new ArrayList();
                dt = new DataTable();
                dt.Columns.Add("1");
                dt.Columns.Add("2");
                dt.Columns.Add("3");
                dt.Columns.Add("4");
    
                for (int i = 0; i < 4; i++)
                {
                    dr = dt.NewRow();
                    dr[0] = num + 1;
                    dr[1] = num + 2;
                    dr[2] = num + 3;
                    dr[3] = num + 4;
    
                    num = num + 4;
    
                    dt.Rows.Add(dr);
                      
                }
    
                TextMessage();
    
                dataGridView1.DataSource = dt;
            }
    
            private void TextMessage()
            {
                for (int i = 0; i < 16; i++)
                {
                    al.Add("第" + Convert.ToString(i + 1) + "题");
                }
    
            }
    
            private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
            {
                textBox1.Text = al[Convert.ToInt32(dataGridView1.CurrentCell.Value.ToString()) -1].ToString();
            }

    <embed height="0" hidden="true" id="ciba_grabword_plugin" type="application/ciba-grabword-plugin" width="0" />

    If this is helpful
      { Please Mark as Answered }
    else if it is not helpful
      { Un-Mark as Answered }


    Best Regards
    Huan Li, Code Blog: Small is New Big In C#

    • 已标记为答案 呆oO 2014年8月16日 3:28
    2012年10月14日 12:13
  •         DataTable dt;
            DataRow dr;
            int num = 0;
            ArrayList al;
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                al = new ArrayList();
                dt = new DataTable();
                dt.Columns.Add("1");
                dt.Columns.Add("2");
                dt.Columns.Add("3");
                dt.Columns.Add("4");
                dt.Columns.Add("5");
                dt.Columns.Add("6");
                dt.Columns.Add("7");
                dt.Columns.Add("8");
                dt.Columns.Add("9");
                dt.Columns.Add("10");
                dt.Columns.Add("11");
                dt.Columns.Add("12");
                dt.Columns.Add("13");
                dt.Columns.Add("14");
                dt.Columns.Add("15");
                dt.Columns.Add("16");
                dt.Columns.Add("17");
                dt.Columns.Add("18");
                dt.Columns.Add("19");
                dt.Columns.Add("20");
    
                for (int i = 0; i < 5; i++)
                {
                    dr = dt.NewRow();
                    dr[0] = num + 1;
                    dr[1] = num +2;
                    dr[2] = num + 3;
                    dr[3] = num + 4;
                    dr[4] = num + 5;
                    dr[5] = num + 6;
                    dr[6] = num + 7;
                    dr[7] = num +8;
                    dr[8] = num + 9;
                    dr[9] = num + 10;
                    dr[10] = num + 11;
                    dr[11] = num + 12;
                    dr[12] = num + 13;
                    dr[13] = num + 14;
                    dr[14] = num + 15;
                    dr[15] = num + 16;
                    dr[16] = num + 17;
                    dr[17] = num + 18;
                    dr[18] = num + 19;
                    dr[19] = num + 20;
    
                    num = num + 20;
    
                    dt.Rows.Add(dr);
    
                }
    
                TextMessage();
    
                dataGridView1.DataSource = dt;
            }
    
            private void TextMessage()
            {
                for (int i = 0; i < 100; i++)
                {
                    al.Add("第" + Convert.ToString(i + 1) + "题");
                }
    
            }
    
            private void dataGridView1_CellClick_1(object sender, DataGridViewCellEventArgs e)
            {
                textBox1.Text = al[Convert.ToInt32(dataGridView1.CurrentCell.Value.ToString()) - 1].ToString();
            }
    
        }
    
    首先,谢谢Li哥给出的示例,我改了下,想实现1-100题的显示,不知道这样该对不对,麻烦大家指点一下,另外还有个问题,如何动态获取长度?比如我读取数据库里有1000条记录,如何显示出来,然后我读另一个数据库里有568条记录,又如何显示出来?麻烦Li哥。。


    C#初学者!~~~

    2012年10月15日 0:07
  •         DataSet ds;
            DataTable dt;
            DataRow dr;
    
            int num = 0;
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                //取出所有题库表Table1里的所有题目
                ds = this.GetDataSet("SELECT ID AS 题号, QUESTION AS 问题 FROM Table1", "题库");
    
                //初始化DataGridView选题田字格
                //ds.Tables[0].Rows.Count / 8 的目的表示 一行上显示几个数字,假定一行显示8个题号
                dt = new DataTable();
                for (int i = 0; i < ds.Tables[0].Rows.Count / 8; i++)  
                {
                    dr = dt.NewRow();
                    dr[0] = num + 1;
                    dr[0] = num + 2;
                    dr[0] = num + 3;
                    dr[0] = num + 4;
                    dr[0] = num + 5;
                    dr[0] = num + 6;
                    dr[0] = num + 7;
                    dr[0] = num + 8;
    
                    num = num + 8;
    
                    dt.Rows.Add(dr);
                }
                dataGridView1.DataSource = dt;
            }
    
    
            private void dataGridView1_CellClick_1(object sender, DataGridViewCellEventArgs e)
            {
                DataView dv;
                DataTable newdt;
                int index;
    
                //获取指定题号
                index = Convert.ToInt32(dataGridView1.CurrentCell.Value.ToString()) - 1;
    
                //从dataset里过滤取出题号对应的问题
                dv = ds.Tables[0].DefaultView;
                dv.RowFilter = "题号 = " + index.ToString() + "";
                newdt = dv.ToTable(true, "问题");
    
                //在textbox显示出问题内容
                textBox1.Text = newdt.Rows[0][0].ToString();
            }
    
            //创建DataSet对象
            public DataSet GetDataSet(string sqlComm, string tableName)
            {
                SqlConnection sqlconn = this.GetSqlConn();
                SqlDataAdapter sqlda = new SqlDataAdapter(sqlComm, sqlconn);
                DataSet ds = new DataSet();
                sqlda.Fill(ds, tableName);
                return ds;
            }
    
            //获取数据库连接 读取config.ini配置文件
            public SqlConnection GetSqlConn()
            {
    
                try
                {
                    string strconn = "user id=sa;data source=. persist security info=True;initial catalog=DBName;password=123456";
                    sqlconn = new SqlConnection(strconn);
                    sqlconn.Open();
                    return sqlconn;
                }
                catch (Exception ex)
                {
                    sqlconn = null;
                    return sqlconn;
                }
            }


    If this is helpful
      { Please Mark as Answered }
    else if it is not helpful
      { Un-Mark as Answered }


    Best Regards
    Huan Li, Code Blog: Small is New Big In C#

    • 已标记为答案 呆oO 2014年8月16日 3:29
    2012年10月15日 2:36
  • 测试了很多遍,还是没成功,您的代码我没看明白从哪里让每一行显示出8个数字的。麻烦把FOR里面的机制讲详细一点,因为我理解能力有限,可能是我基础太差了,麻烦Li哥给多加点注释,或者换一个方法,再或者麻烦您建个ACCESS数据库重写一下代码,谢谢!

    C#初学者!~~~

    2012年10月15日 13:12

  • <embed height="0" hidden="true" id="ciba_grabword_plugin" type="application/ciba-grabword-plugin" width="0" />以你的截图为例子吧,一共80道题,一行10个题号共8行


    ds.Tables[0].Rows.Count / 10  除10的目的是算每行10个题,一共需要几行格子才能显示的完
    80道题/10 就是8行,跟你图的一样

                for (int i = 0; i < ds.Tables[0].Rows.Count / 10; i++)  
                {
                    dr = dt.NewRow();
                    
                    //下面这个是给每一行的1-10格子赋值题号
                    dr[0] = num + 1;
                    dr[0] = num + 2;
                    dr[0] = num + 3;
                    dr[0] = num + 4;
                    dr[0] = num + 5;
                    dr[0] = num + 6;
                    dr[0] = num + 7;
                    dr[0] = num + 8;
                    dr[0] = num + 9;
                    dr[0] = num + 10;
                    一行的10个格子赋值完成后就添加到dt里
                    dt.Rows.Add(dr);
                    
                    
                                //这句的意思是下一行的题号是从几开始
                    num = num + 10;
                   

                }


    If this is helpful
      { Please Mark as Answered }
    else if it is not helpful
      { Un-Mark as Answered }


    Best Regards
    Huan Li, Code Blog: Small is New Big In C#

    2012年10月15日 13:29
  • 可能是我太愚笨,我再次尝试了一下,还是没能成功。

    这是SQL语句:

    SELECT tm as 题目,tmlx as 题目类型,daA as 答案A,daB as 答案B,daC as 答案C,daD as 答案D,daE as 答案E,daF as 答案F,daG as 答案G,zqda as 正确答案,ZJB.zjmc as 所属章节,picpath as 图片路径,szym as 所在页码,daanshu as 答案数,daantishi as 答案提示,cxb.cxmc as 车型 FROM TMB,ZJB,CXB where tmb.zjmc = zjb.zjmc and tmb.cxmc = cxb.cxmc and tmb.cxmc='大型客车' order by tmid

    下面是出错提示:

    下面是代码 :

                        string sql = string.Format("SELECT tm as 题目,tmlx as 题目类型,daA as 答案A,daB as 答案B,daC as 答案C,daD as 答案D,daE as 答案E,daF as 答案F,daG as 答案G,zqda as 正确答案,ZJB.zjmc as 所属章节,picpath as 图片路径,szym as 所在页码,daanshu as 答案数,daantishi as 答案提示,cxb.cxmc as 车型 FROM TMB,ZJB,CXB where tmb.zjmc = zjb.zjmc and tmb.cxmc = cxb.cxmc and tmb.cxmc='大型客车' order by tmid");
                        //DataTable dt1 = new ConnDb().dt(sql);
                        //ds = this.GetDataSet("SELECT tm as 题目,tmlx as 题目类型,daA as 答案A,daB as 答案B,daC as 答案C,daD as 答案D,daE as 答案E,daF as 答案F,daG as 答案G,zqda as 正确答案,ZJB.zjmc as 所属章节,picpath as 图片路径,szym as 所在页码,daanshu as 答案数,daantishi as 答案提示,cxb.cxmc as 车型 FROM TMB,ZJB,CXB where tmb.zjmc = zjb.zjmc and tmb.cxmc = cxb.cxmc and tmb.cxmc='大型客车' order by tmid", "tmb");
                        ds = new ConnDb().ds(sql);
                        dt1 = new DataTable();
                        for (int i = 0; i < ds.Tables[0].Rows.Count / 10; i++)
                        {
                            dr1 = dt1.NewRow();
                            dr1[0] = num + 1;
                            dr1[0] = num + 2;
                            dr1[0] = num + 3;
                            dr1[0] = num + 4;
                            dr1[0] = num + 5;
                            dr1[0] = num + 6;
                            dr1[0] = num + 7;
                            dr1[0] = num + 8;
                            dr1[0] = num + 9;
                            dr1[0] = num + 10;
    
                            num = num + 10;
                            dt1.Rows.Add(dr1);
                        }
                        dgv_timuID.DataSource = dt1;
    
    
    
    
    Class.cs
    
            //打开数据库
            public ConnDb()
            {
                if (conn == null)
                {
                    conn = new OleDbConnection();
                    conn.ConnectionString = @"provider = microsoft.jet.oledb.4.0;data source=data\data.mdb;Jet OLEDB:Database Password=12345"; 
                }
                if (conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }
            }
    
            //从数据库中查找数据
            public DataSet ds(string sql)
            {
                DataSet ds = new DataSet();
                OleDbDataAdapter da = new OleDbDataAdapter();
                //com.CommandText = sql;
                //com.CommandType = CommandType.Text;
                OleDbCommand cmd = new OleDbCommand(sql, conn);
                da.SelectCommand = cmd;
                da.Fill(ds);
                connClose();
                return ds;
            }
            //从数据库中读取表
            public DataTable dt(string sql)
            {
                DataTable dt = new DataTable();
                OleDbDataAdapter da = new OleDbDataAdapter();
                OleDbCommand cmd = new OleDbCommand(sql, conn);
                da.SelectCommand = cmd;
                da.Fill(dt);
                conn.Close();
                return dt;
            }
    
            public OleDbDataReader dr(string sql)
            {
                OleDbDataReader dr = null;
                OleDbConnection conn = new OleDbConnection(@"provider = microsoft.jet.oledb.4.0;data source=data\data.mdb;Jet OLEDB:Database Password=leonahe");
                OleDbCommand cmd = new OleDbCommand(sql, conn);
                conn.Open();
                dr = cmd.ExecuteReader();
                connClose();//关闭连接
                return dr;//返回一个影响行数
            }
    
            public OleDbCommand oc(string sql)
            {
                OleDbCommand oc = new OleDbCommand(sql, conn);
                OleDbDataReader odr = oc.ExecuteReader();
    
                connClose();
                return oc;
            }
    
            //对数据库进行更新
            public int updata(string sql)
            {
                OleDbCommand oc = new OleDbCommand();//表示要对数据源执行的SQL语句或存储过程
                oc.CommandText = sql;//设置命令的文本
                oc.CommandType = CommandType.Text;//设置命令的类型
                oc.Connection = conn;//设置命令的连接
                int x = oc.ExecuteNonQuery();//执行SQL语句
                connClose();//关闭连接
                return x;//返回一个影响行数
            }
    
    
    
    
            //关闭数据库链接
            public void connClose()
            {
                if (conn.State == ConnectionState.Open)
                {
                    conn.Close();
                }
            }
    
    
    为什么读不到数据?


    C#初学者!~~~




    • 已编辑 呆oO 2012年10月24日 1:02
    2012年10月24日 0:57
  • 东哥在不在,帮忙给看一下。谢谢!


    C#初学者!~~~

    2012年10月25日 6:04

  • <embed height="0" hidden="true" id="ciba_grabword_plugin" type="application/ciba-grabword-plugin" width="0" />dt1 = new datatable()这句不对

    应该是dt1= dt.Table[0]


    If this is helpful
      { Please Mark as Answered }
    else if it is not helpful
      { Un-Mark as Answered }


    Best Regards
    Huan Li, Code Blog: Small is New Big In C#

    • 已标记为答案 呆oO 2014年8月16日 3:29
    2012年11月5日 8:16