步骤1:在microsoft visual studio中创建新项目
启动microsoft visual studio,并创建新项目,选择 windows窗体应用程序记住项目类型为 visual c#。您可以根据需要为项目命名,如果有必要,可以更改项目的位置。
步骤2:将所有内容添加到表单
现在,它是一个空表格。让我们在工具箱中添加一些组件,如图所示。它们是:datagridview,savefiledioalog,openfiledialog和menustrip
第3步:添加列
添加datagridview之后,我们有空的空间,在其上单击鼠标右键,然后选择编辑列。
步骤4:编写代码之前
确保表单看起来像这样,datagridview名为“grid”。您可以在适当的窗口中设置它
步骤5:编写代码
在菜单的每个元素上单击两次,以创建事件,每次显示带代码的窗口时,请执行返回并使用所有(保存,打开,关闭)
我们需要的代码:
private void savetoolstripmenuitem_click(object sender,eventargs e)
{
}
private void opentoolstripmenuitem_click (object sender,eventargs e)
{
}
private void closetoolstripmenuitem_click(object sender,eventargs e)
{
}
步骤6:代码代码。..。
以下是我们的应用程序的完整代码,其中包含“//”情况后的注释:
using system;
using system.collections.generic;
使用system.componentmodel;
使用system.data;
使用system.drawing;
使用system.linq;
使用system.text;
使用system.io; //添加
使用system.windows.forms;
使用system.runtime.serialization.formatters.binary; //添加
使用system。 runtime.serialization; //添加
命名空间testowa //这是我的项目名称
{
公共部分类form1:表单
public form1()
{
initializecomponent();
}
[serializable] //它允许我们的类保存在文件中
公共类数据//我们的数据类
{
公共字符串名称;
公共字符串姓氏;
公共字符串城市;
公共字符串编号;
}
private void savetoolstripmenuitem_click(object sender,eventargs e)
{
grid.endedit();
savefiledialog savefiledialog1 = new savefiledialog(); //创建文件保存对话框
savefiledialog1.restoredirectory = true;
//读取并过滤原始数据
if(savefiledialog1.showdialog( )== dialogresult.ok)
{
binaryformatter formatter = new binaryformatter();
filestream output = new filestream(savefiledialog1.filename,filemode.openorcreate,fileaccess.write);
‘
int n = grid.rowcount;
data [] person =新数据[n - 1]; //我们拥有尽可能多的行记录,自动添加行,所以我们总共有一行比我们需要的多,所以n是行数-1空行
for(int i = 0; i person [i] = new data();
//grid在“[]”中有两个数字第一个数字是一个列的索引,第二个是行’的idnex,索引总是从0开始
person [i] .name = grid [0,i] .value.tostring();
person [i] .surname = grid [1,i] .value.tostring();
person [i] .city = grid [2,i] .value.tostring();
person [i]。 number = grid [3,i] .value.tostring();
}
formatter.serialize(output,person);
output.close();
}
}
private void opentoolstripmenuitem_click(object sender,eventargs e)//读取文件并将数据添加到grid
{
openfiledialog1 = new openfiledialog();
if(openfiledialog1.showdialog ()== dialogresult.ok)
{
binaryformatter reader = new binaryformatter();
filestream input = new filestream(openfiledialog1。 filename,filemode.open,fileaccess.read);
data [] person =(data [])reader.deserialize(input);
grid.rows.clear();
for(int i = 0; i {
grid.rows.add();
grid [0,i] .value = person [i]。 name;
grid [1,i] .value = person [i] .surname;
grid [2,i] .value = person [i] .city;
grid [3,i]。 value = person [i] .number;
}
private void closetoolstripmenuitem_click(object sender,eventargs e)
{
close(); //关闭应用
第7步:完成了。测试它
单击visual studio中的菜单debug比启动调试它应该工作。尝试测试应用程序。我相信你会发现一些错误,我们的应用程序非常简单,没有任何证据,同时保存,打开文件,它只是演示如何做一个更大的有用的应用程序。 你可以改进它,制作你自己的版本! 添加一些新功能,随意更改。祝你好运!
详解PCB高速信号回流路径
数据中心钛基准性能优于基于GaN的3kW AC/DC PSU
充分发挥人工智能赋能效用,助力疫情防控和复工复产
一文带你看透射频无源器件的那些“秘密”
iPhone 12性能翻车?A14处理器跑分竟比不过高通骁龙865?
怎样在MicrosoftVisualStudio中创建一个简单的电话簿应用程序
纸张表面缺陷检测原理、技术参数及功能
苹果发布iPhone 15 Pro和Pro Max 搭载A17 Pro芯片
Kindle为什么会在中国如此成功?
会议厅led电子屏应用的四种解决方法及保养技巧
ADI LTC6806燃料电池电堆巡检的应用场景
安森美推出以太网供电(PoE)和KNX系统方案
浅析安裝无功功率补偿设备的益处
企业排污排水管网流量水位在线监测设备功能
【技术大咖测试笔记系列】之十一:使用锁定热成像技术加快故障分析
西门子一马当先 解密工业数字化的未来图景
什么样的城市才是智慧城市?
vivoAPEX2019渲染图曝光 将是2019年首款极具创新性旗舰产品
庞大的 Linux 内核项目,如何保持30年来有条不紊地运转
整车高低温湿热环境试验室:严苛环境下汽车性能与可靠性的终极考验