C#中可调整大小的datagridview [英] Resizable datagridview in C#

查看:609
本文介绍了C#中可调整大小的datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有一个场景,我需要创建一个只有1行的表。我不应该为此使用数据库。我有一个很长的字符串,我应该将其子串并放到这一行的每一列。这个表应该可以调整长度。

这是它的列应该根据我的输入增加或减少。有没有人有任何想法。



我尝试过:



我更喜欢使用没有数据库的datagrid视图。我的数据列及其标题都应根据我的输入长度而有所不同。

Hi,

I have a scenario in which I need to create a table that has only 1 row. I am not supposed to use database for this. I have a long string of bytes which i should substring and put to each column of this row.This table should be resizable in terms of length.
That is its columns should increase or decrease based on my input. Has anyone got any idea.

What I have tried:

I preferred using datagrid view without database. Both my data column and its title should vary based on my input length.

DataTable dt1 = new DataTable();
//i add columns likethis
dt1.Columns.AddRange(new DataColumn[4]{new DataColumn("byte1",typeof(string)),new DataColumn("byte2",typeof(string)),new DataColumn("byte3",typeof(string)),new DataColumn("byte4",typeof(string))});

int j=0;
byte0=hexstring.Substring(j,2);
.
.
.
//after i create columns i add data to rows like the following
dt1.Rows.Add(byte0,byte1,byte2,byte3);





Add中的这些值会根据我的输入继续改变说我有10个字节,它与byte9相关。我怎样才能让我变得通用。是否有任何解决方案比datagrid视图?任何帮助表示赞赏。



These values inside Add keep on changing based on my input say i have 10 bytes, it inreases to byte9. How can I make i generic. Is there any solution ither than datagrid view? Any help appreciated.

推荐答案

我不确定我是否理解你,但是...



看看下面的方法:

I'm not sure i understand you well, but...

Take a look at below method:
public string[] ToHexString(string input)
{
	return input.Select(c=>((int)c).ToString("X2")).ToArray();
}



它将字符串转换为字符串中每个字符的十六进制表示数组。现在,您可以简单地创建DataTable。请参阅:


It converts string into array of "hex-representation" of each char in string. Now, you can simply create DataTable. See:

string s = "not very long string";
DataTable dt = new DataTable();
string[] hexstring = ToHexString(s);
for(int i=0; i<hexstring.Length; i++)
{
	dt.Columns.Add(new DataColumn(string.Format("byte{0}", i), typeof(string)));
}
dt.Rows.Add(hexstring);

//bind data to DataGridView
dataGridView1.Datasource = dt;





祝你好运!



Good luck!


这篇关于C#中可调整大小的datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆