C# WinForms DataGridView不滚动,但启动另一列
以上是我在WindForms中的DataGridView。但我不希望它滚动。我想要的是,当行从上到下适合面板时,将从顶部在右侧创建新行。预期的版本应如下图所示C# WinForms DataGridView不滚动,但启动另一列,c#,winforms,datagridview,C#,Winforms,Datagridview,以上是我在WindForms中的DataGridView。但我不希望它滚动。我想要的是,当行从上到下适合面板时,将从顶部在右侧创建新行。预期的版本应如下图所示 而且,当我更改窗口的大小时,数据行应该自动重新排列以适应面板。我不知道该怎么做,在搜索了所有与DataGridView相关的问题后也没有得到任何线索。请提供您的想法或意见。非常感谢 下面是一个使用具有动态添加列数的数据表的粗略示例: public class StockInfo { public String Symbol;
而且,当我更改窗口的大小时,数据行应该自动重新排列以适应面板。我不知道该怎么做,在搜索了所有与DataGridView相关的问题后也没有得到任何线索。请提供您的想法或意见。非常感谢 下面是一个使用具有动态添加列数的
数据表的粗略示例:
public class StockInfo {
public String Symbol;
public decimal Price;
public decimal Change;
}
public class StockForm : Form {
SprawlingDGV dgv = new SprawlingDGV();
public StockForm() {
Controls.Add(dgv);
}
}
public class SprawlingDGV : DataGridView {
DataTable table = new DataTable();
int previousNumRowsAvailable = -1;
public List<StockInfo> StockData = new List<StockInfo>();
public SprawlingDGV() {
Dock = DockStyle.Fill;
DataSource = table;
AllowUserToAddRows = false;
for (int i = 0; i < 50; i++) {
StockInfo s = new StockInfo();
s.Symbol = "Symbol" + i;
s.Price = i;
StockData.Add(s);
}
}
protected override void OnCellFormatting(DataGridViewCellFormattingEventArgs e) {
base.OnCellFormatting(e);
// TBD: Hide the grid lines for unused cells
if (e.Value == DBNull.Value)
e.CellStyle.BackColor = Color.DarkGray;
}
protected override void OnResize(EventArgs e) {
base.OnResize(e);
DataTable table2 = new DataTable();
var col1 = table2.Columns.Add("Symbol_0", typeof(String));
var col2 = table2.Columns.Add("Price_0", typeof(decimal));
var col3 = table2.Columns.Add("Change_0", typeof(decimal));
int heightPerRow = 30; // TBD: determine height based on Font
int numRowsAvailable = (this.Height - this.ColumnHeadersHeight) / heightPerRow;
if (numRowsAvailable == previousNumRowsAvailable)
return;
previousNumRowsAvailable = numRowsAvailable;
int n = Math.Max(1, numRowsAvailable);
int id = 0;
for (int i = 0, k = 0; i < StockData.Count; i++) {
StockInfo s = StockData[i];
DataRow row = null;
if (id == 0) {
row = table2.NewRow();
table2.Rows.Add(row);
}
else {
row = table2.Rows[k];
}
row[col1] = s.Symbol;
row[col2] = s.Price;
row[col3] = s.Change;
k++;
if (k == n) {
k = 0;
id++;
col1 = table2.Columns.Add("Symbol_" + id, typeof(String));
col2 = table2.Columns.Add("Price_" + id, typeof(decimal));
col3 = table2.Columns.Add("Change_" + id, typeof(decimal));
}
}
DataSource = table2;
table.Dispose();
table = table2;
foreach (DataGridViewColumn c in Columns) {
String t = c.HeaderText;
int x = t.IndexOf('_');
if (x < 0)
continue;
c.HeaderText = t.Substring(0, x);
}
}
}
公共类股票信息{
公共字符串符号;
公共十进制价格;
公共小数变更;
}
公共类股票:表格{
SprawlingDGV dgv=新的SprawlingDGV();
公共股票形式(){
控件。添加(dgv);
}
}
公共类扩展DGV:DataGridView{
DataTable=新的DataTable();
int-previousNumRowsAvailable=-1;
public List StockData=新列表();
公共蔓延dgv(){
Dock=DockStyle.Fill;
数据源=表格;
AllowUserToAddress=false;
对于(int i=0;i<50;i++){
StockInfo s=新的StockInfo();
s、 Symbol=“Symbol”+i;
s、 价格=i;
股票数据。添加;
}
}
受保护的重写void OnCellFormatting(DataGridViewCellFormattingEventArgs e){
base.oncell格式(e);
//TBD:隐藏未使用单元格的网格线
if(e.Value==DBNull.Value)
e、 CellStyle.BackColor=Color.DarkGray;
}
受保护的覆盖void OnResize(事件参数e){
基数(e);
DataTable2=新的DataTable();
var col1=table2.Columns.Add(“Symbol_0”,typeof(String));
var col2=表2.Columns.Add(“Price_0”,typeof(decimal));
var col3=table2.Columns.Add(“Change_0”,typeof(decimal));
int heightPerRow=30;//待定:根据字体确定高度
int numRowsAvailable=(this.Height-this.columnheadershight)/heightPerRow;
if(numRowsAvailable==previousNumRowsAvailable)
返回;
previousNumRowsAvailable=numRowsAvailable;
int n=Math.Max(1,numRowsAvailable);
int id=0;
for(inti=0,k=0;i
这是一个很好的解决方案。我尝试在同一个面板中使用几个DataGridView来达到效果,但失败了。我将再次尝试这两种解决方案,非常感谢您的分享。@Louis使用多个DataGridView
对象进行同步会很困难,只使用一个对象会容易得多。谢谢您的评论。你是对的。使用多个DataGridView
对象确实很困难。所以我要按照你的解决方案来尝试一下。再次感谢!谢谢你的编辑。