将列数据显示在gridview中

.Net技术 码拜 8年前 (2016-04-30) 894次浏览
高手们好,本人想将sd.xxxxxx分列显示在datagridview中的列中,估计存储了一年的数据有大约400行。
怎么把全部sd都存放在一个datagridview中,这里datagridview.datasource怎么指定?不指定可以直接添加列数据吗?谢谢
帮忙写下代码,谢谢了。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace stockanalysis
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string filePath = @”C:\同花顺软件\同花顺\history\sznse\day\300014.day”;//同花顺目录下history目录是历史日线数据,本人例子打开的是平安银行000001的
byte[] stockFileBytes = System.IO.File.ReadAllBytes(filePath);
int recordStartPos = readByteToInt(stockFileBytes, 10, 2);//记录开始位置
int recordLength = readByteToInt(stockFileBytes, 12, 2);//记录长度
int recordCount = readByteToInt(stockFileBytes, 14, 2);//文件中记录条数
int fileBytesLength = stockFileBytes.Length;
int pos = recordStartPos;
List<string> stockDayList = new List<string>();//日线数据暂时读到List中
do
{
StockDay sd = new StockDay();
sd.DateInt = readByteToInt(stockFileBytes, pos, 4);//时间,整形表示的,可转为日期型
sd.OpenPrice = readByteToInt(stockFileBytes, pos + 4, 2) * 0.001f;//开盘价
sd.HighPrice = readByteToInt(stockFileBytes, pos + 8, 2) * 0.001f;//最高价
sd.LowPrice = readByteToInt(stockFileBytes, pos + 12, 2) * 0.001f;//最低价
sd.ClosePrice = readByteToInt(stockFileBytes, pos + 16, 2) * 0.001f;//收盘价
sd.VolumeValue = readByteToInt(stockFileBytes, pos + 20, 4);//成交额
sd.Volume = readByteToInt(stockFileBytes, pos + 24, 4);//成交量
stockDayList.Add(sd.ToString());
pos = pos + recordLength;

for (int i = 0; i < recordCount -1; i++)
{

//dataGridView1.Rows[i].Cells[1].Value = TotalSeconds;
//dataGridView1.Rows[i].Cells[2].Value = textBox5.Text;
//dataGridView1.Rows.Add();
dataGridView1.Rows[i].Cells[0].Value = sd.DateInt;
//dataGridView1.Rows[i].Cells[2].Value = sd.OpenPrice;
//dataGridView1.Rows[i].Cells[3].Value = sd.HighPrice;
//dataGridView1.Rows[i].Cells[3].Value = “A”;

}

} while (pos < fileBytesLength);

}
// 用到了如下两个方法:
///
/// 读取某位置开始的byte转换为16进制字符串
///
///
///
///
private string readByteToHex(byte[] stockFileBytes, int startPos, int length)
{
string r = “”;
for (int i = startPos + length – 1; i >= startPos; i–)
{
r += stockFileBytes[i].ToString(“X2”);
}
return r;
}
///
/// 读取某位置开始的byte转换为16进制字符串
///
///
///
///
private int readByteToInt(byte[] stockFileBytes, int startPos, int length)
{
string r = readByteToHex(stockFileBytes, startPos, length);
int v = Convert.ToInt32(r, 16);
return v;
}
// 每一天的日K线封装为 类
public class StockDay
{
int dateInt;
public int DateInt
{
get { return dateInt; }
set { dateInt = value; }
}
DateTime date;//日期
public DateTime Date
{
get { return date; }
set { date = value; }
}
float openPrice;//开盘价
public float OpenPrice
{
get { return openPrice; }
set { openPrice = value; }
}
float closePrice;//收盘价
public float ClosePrice
{
get { return closePrice; }
set { closePrice = value; }
}
float highPrice;//最高价
public float HighPrice
{
get { return highPrice; }
set { highPrice = value; }
}
float lowPrice;//最低价
public float LowPrice
{
get { return lowPrice; }
set { lowPrice = value; }
}
float volume;//成交量
public float Volume
{
get { return volume; }
set { volume = value; }
}
float volumeValue;//成交额
public float VolumeValue
{
get { return volumeValue; }
set { volumeValue = value; }
}
}

}
}

解决方案

100

假设你查询出来的在 datatable中

int i = 0;
foreach (var item in dt.Colums.Count())
{
dgv.Rows.Add(new string[] { dt[i++] });
}

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明将列数据显示在gridview中
喜欢 (0)
[1034331897@qq.com]
分享 (0)