c# - "The name 'GRID' does not exist in the current context" error -
- list item
hey guys have been doing first ever windows forms/c# application , cannot rid of error. i've dragged , dropped data grid view form , renamed grid , problem persists. here code:
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.io; //added using system.windows.forms; using system.runtime.serialization.formatters.binary; //added using system.runtime.serialization; //added namespace testowa // name of project { public partial class form1 : form { public form1() { initializecomponent(); } private void initializecomponent() { throw new notimplementedexception(); } [serializable] // allow our class saved in file public class data // our class data { public string name; public string surname; public string city; public string number; } private void savetoolstripmenuitem_click(object sender, eventargs e) { grid.endedit(); savefiledialog savefiledialog1 = new savefiledialog(); //creating file save dialog savefiledialog1.restoredirectory = true; //read , filter raw data 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 = new data[n - 1]; //we have many records many rows, rows added automaticly have 1 row more need, n number of rows -1 empty row (int = 0; < n - 1; i++) { person[i] = new data(); //grid has 2 numbers in"[]" first numer index of column, second idnex of row', indexing starts 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) // reading file , adding data 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(); (int = 0; < person.length; 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(); // closing app } public openfiledialog openfiledialog1 { get; set; } } }
what should do? thanks.
form1.designer
namespace windowsformsapplication7 { partial class form1 { /// <summary> /// required designer variable. /// </summary> private system.componentmodel.icontainer components = null; /// <summary> /// clean resources being used. /// </summary> /// <param name="disposing">true if managed resources should disposed; otherwise, false.</param> protected override void dispose(bool disposing) { if (disposing && (components != null)) { components.dispose(); } base.dispose(disposing); } #region windows form designer generated code /// <summary> /// required method designer support - not modify /// contents of method code editor. /// </summary> private void initializecomponent() { this.menustrip1 = new system.windows.forms.menustrip(); this.filetoolstripmenuitem = new system.windows.forms.toolstripmenuitem(); this.savetoolstripmenuitem = new system.windows.forms.toolstripmenuitem(); this.opentoolstripmenuitem = new system.windows.forms.toolstripmenuitem(); this.closetoolstripmenuitem = new system.windows.forms.toolstripmenuitem(); this.openfiledialog1 = new system.windows.forms.openfiledialog(); this.grid = new system.windows.forms.datagridview(); this.column1 = new system.windows.forms.datagridviewtextboxcolumn(); this.column2 = new system.windows.forms.datagridviewtextboxcolumn(); this.column3 = new system.windows.forms.datagridviewtextboxcolumn(); this.column4 = new system.windows.forms.datagridviewtextboxcolumn(); this.savefiledialog1 = new system.windows.forms.savefiledialog(); this.menustrip1.suspendlayout(); ((system.componentmodel.isupportinitialize)(this.grid)).begininit(); this.suspendlayout(); // // menustrip1 // this.menustrip1.items.addrange(new system.windows.forms.toolstripitem[] { this.filetoolstripmenuitem}); this.menustrip1.location = new system.drawing.point(0, 0); this.menustrip1.name = "menustrip1"; this.menustrip1.size = new system.drawing.size(651, 24); this.menustrip1.tabindex = 0; this.menustrip1.text = "menustrip1"; // // filetoolstripmenuitem // this.filetoolstripmenuitem.dropdownitems.addrange(new system.windows.forms.toolstripitem[] { this.savetoolstripmenuitem, this.opentoolstripmenuitem, this.closetoolstripmenuitem}); this.filetoolstripmenuitem.name = "filetoolstripmenuitem"; this.filetoolstripmenuitem.size = new system.drawing.size(37, 20); this.filetoolstripmenuitem.text = "file"; // // savetoolstripmenuitem // this.savetoolstripmenuitem.name = "savetoolstripmenuitem"; this.savetoolstripmenuitem.size = new system.drawing.size(152, 22); this.savetoolstripmenuitem.text = "save"; this.savetoolstripmenuitem.click += new system.eventhandler(this.savetoolstripmenuitem_click); // // opentoolstripmenuitem // this.opentoolstripmenuitem.name = "opentoolstripmenuitem"; this.opentoolstripmenuitem.size = new system.drawing.size(152, 22); this.opentoolstripmenuitem.text = "open"; this.opentoolstripmenuitem.click += new system.eventhandler(this.opentoolstripmenuitem_click); // // closetoolstripmenuitem // this.closetoolstripmenuitem.name = "closetoolstripmenuitem"; this.closetoolstripmenuitem.size = new system.drawing.size(152, 22); this.closetoolstripmenuitem.text = "close"; this.closetoolstripmenuitem.click += new system.eventhandler(this.closetoolstripmenuitem_click); // // openfiledialog1 // this.openfiledialog1.filename = "openfiledialog1"; this.openfiledialog1.fileok += new system.componentmodel.canceleventhandler(this.openfiledialog1_fileok); // // grid // this.grid.columnheadersheightsizemode = system.windows.forms.datagridviewcolumnheadersheightsizemode.autosize; this.grid.columns.addrange(new system.windows.forms.datagridviewcolumn[] { this.column1, this.column2, this.column3, this.column4}); this.grid.location = new system.drawing.point(13, 28); this.grid.name = "grid"; this.grid.size = new system.drawing.size(451, 232); this.grid.tabindex = 1; this.grid.cellcontentclick += new system.windows.forms.datagridviewcelleventhandler(this.datagridview1_cellcontentclick); // // column1 // this.column1.headertext = "name"; this.column1.name = "column1"; // // column2 // this.column2.headertext = "last name"; this.column2.name = "column2"; // // column3 // this.column3.headertext = "city"; this.column3.name = "column3"; // // column4 // this.column4.headertext = "phone #"; this.column4.name = "column4"; // // form1 // this.autoscaledimensions = new system.drawing.sizef(6f, 13f); this.autoscalemode = system.windows.forms.autoscalemode.font; this.clientsize = new system.drawing.size(651, 262); this.controls.add(this.grid); this.controls.add(this.menustrip1); this.mainmenustrip = this.menustrip1; this.name = "form1"; this.text = "form1"; this.menustrip1.resumelayout(false); this.menustrip1.performlayout(); ((system.componentmodel.isupportinitialize)(this.grid)).endinit(); this.resumelayout(false); this.performlayout(); } #endregion private system.windows.forms.menustrip menustrip1; private system.windows.forms.toolstripmenuitem filetoolstripmenuitem; private system.windows.forms.toolstripmenuitem savetoolstripmenuitem; private system.windows.forms.toolstripmenuitem opentoolstripmenuitem; private system.windows.forms.toolstripmenuitem closetoolstripmenuitem; private system.windows.forms.openfiledialog openfiledialog1; private system.windows.forms.datagridview grid; private system.windows.forms.datagridviewtextboxcolumn column1; private system.windows.forms.datagridviewtextboxcolumn column2; private system.windows.forms.datagridviewtextboxcolumn column3; private system.windows.forms.datagridviewtextboxcolumn column4; private system.windows.forms.savefiledialog savefiledialog1; } }
looks have namespace of testowa
form1.cs file, windowsapplication7 form1.designer.cs. try matching them up.
also remove code snippet form1.cs
private void initializecomponent() { throw new notimplementedexception(); }
and code snippet
public openfiledialog openfiledialog1 { get; set; }
also, need make event handlers match in designer
savetoolstripmenuitem_click
should be
savetoolstripmenuitem_click
and on , forth.
finally add handlers openfiledialog_fileok
, cellcontentclick
in form1.cs
void openfiledialog1_fileok(object sender, system.componentmodel.canceleventargs e) { } void datagridview1_cellcontentclick(object sender, datagridviewcelleventargs e) { }
edit
form1.cs
using system; using system.io; //added using system.windows.forms; using system.runtime.serialization.formatters.binary; //added //added namespace testowa // name of project { public partial class form1 : form { public form1() { initializecomponent(); } [serializable] // allow our class saved in file public class data // our class data { public string name; public string surname; public string city; public string number; } private void savetoolstripmenuitem_click(object sender, eventargs e) { grid.endedit(); savefiledialog savefiledialog1 = new savefiledialog(); //creating file save dialog savefiledialog1.restoredirectory = true; //read , filter raw data 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 = new data[n - 1]; //we have many records many rows, rows added automaticly have 1 row more need, n number of rows -1 empty row (int = 0; < n - 1; i++) { person[i] = new data(); //grid has 2 numbers in"[]" first numer index of column, second idnex of row', indexing starts 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) // reading file , adding data 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(); (int = 0; < person.length; 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(); // closing app } void openfiledialog1_fileok(object sender, system.componentmodel.canceleventargs e) { } void datagridview1_cellcontentclick(object sender, datagridviewcelleventargs e) { } } }
form1.designer.cs
namespace testowa { partial class form1 { /// <summary> /// required designer variable. /// </summary> private system.componentmodel.icontainer components = null; /// <summary> /// clean resources being used. /// </summary> /// <param name="disposing">true if managed resources should disposed; otherwise, false.</param> protected override void dispose(bool disposing) { if (disposing && (components != null)) { components.dispose(); } base.dispose(disposing); } #region windows form designer generated code /// <summary> /// required method designer support - not modify /// contents of method code editor. /// </summary> private void initializecomponent() { this.menustrip1 = new system.windows.forms.menustrip(); this.filetoolstripmenuitem = new system.windows.forms.toolstripmenuitem(); this.savetoolstripmenuitem = new system.windows.forms.toolstripmenuitem(); this.opentoolstripmenuitem = new system.windows.forms.toolstripmenuitem(); this.closetoolstripmenuitem = new system.windows.forms.toolstripmenuitem(); this.openfiledialog1 = new system.windows.forms.openfiledialog(); this.grid = new system.windows.forms.datagridview(); this.column1 = new system.windows.forms.datagridviewtextboxcolumn(); this.column2 = new system.windows.forms.datagridviewtextboxcolumn(); this.column3 = new system.windows.forms.datagridviewtextboxcolumn(); this.column4 = new system.windows.forms.datagridviewtextboxcolumn(); this.savefiledialog1 = new system.windows.forms.savefiledialog(); this.menustrip1.suspendlayout(); ((system.componentmodel.isupportinitialize)(this.grid)).begininit(); this.suspendlayout(); // // menustrip1 // this.menustrip1.items.addrange(new system.windows.forms.toolstripitem[] { this.filetoolstripmenuitem}); this.menustrip1.location = new system.drawing.point(0, 0); this.menustrip1.name = "menustrip1"; this.menustrip1.size = new system.drawing.size(651, 24); this.menustrip1.tabindex = 0; this.menustrip1.text = "menustrip1"; // // filetoolstripmenuitem // this.filetoolstripmenuitem.dropdownitems.addrange(new system.windows.forms.toolstripitem[] { this.savetoolstripmenuitem, this.opentoolstripmenuitem, this.closetoolstripmenuitem}); this.filetoolstripmenuitem.name = "filetoolstripmenuitem"; this.filetoolstripmenuitem.size = new system.drawing.size(37, 20); this.filetoolstripmenuitem.text = "file"; // // savetoolstripmenuitem // this.savetoolstripmenuitem.name = "savetoolstripmenuitem"; this.savetoolstripmenuitem.size = new system.drawing.size(152, 22); this.savetoolstripmenuitem.text = "save"; this.savetoolstripmenuitem.click += new system.eventhandler(this.savetoolstripmenuitem_click); // // opentoolstripmenuitem // this.opentoolstripmenuitem.name = "opentoolstripmenuitem"; this.opentoolstripmenuitem.size = new system.drawing.size(152, 22); this.opentoolstripmenuitem.text = "open"; this.opentoolstripmenuitem.click += new system.eventhandler(this.opentoolstripmenuitem_click); // // closetoolstripmenuitem // this.closetoolstripmenuitem.name = "closetoolstripmenuitem"; this.closetoolstripmenuitem.size = new system.drawing.size(152, 22); this.closetoolstripmenuitem.text = "close"; this.closetoolstripmenuitem.click += new system.eventhandler(this.closetoolstripmenuitem_click); // // openfiledialog1 // this.openfiledialog1.filename = "openfiledialog1"; this.openfiledialog1.fileok += new system.componentmodel.canceleventhandler(this.openfiledialog1_fileok); // // grid // this.grid.columnheadersheightsizemode = system.windows.forms.datagridviewcolumnheadersheightsizemode.autosize; this.grid.columns.addrange(new system.windows.forms.datagridviewcolumn[] { this.column1, this.column2, this.column3, this.column4}); this.grid.location = new system.drawing.point(13, 28); this.grid.name = "grid"; this.grid.size = new system.drawing.size(451, 232); this.grid.tabindex = 1; this.grid.cellcontentclick += new system.windows.forms.datagridviewcelleventhandler(this.datagridview1_cellcontentclick); // // column1 // this.column1.headertext = "name"; this.column1.name = "column1"; // // column2 // this.column2.headertext = "last name"; this.column2.name = "column2"; // // column3 // this.column3.headertext = "city"; this.column3.name = "column3"; // // column4 // this.column4.headertext = "phone #"; this.column4.name = "column4"; // // form1 // this.autoscaledimensions = new system.drawing.sizef(6f, 13f); this.autoscalemode = system.windows.forms.autoscalemode.font; this.clientsize = new system.drawing.size(651, 262); this.controls.add(this.grid); this.controls.add(this.menustrip1); this.mainmenustrip = this.menustrip1; this.name = "form1"; this.text = "form1"; this.menustrip1.resumelayout(false); this.menustrip1.performlayout(); ((system.componentmodel.isupportinitialize)(this.grid)).endinit(); this.resumelayout(false); this.performlayout(); } #endregion private system.windows.forms.menustrip menustrip1; private system.windows.forms.toolstripmenuitem filetoolstripmenuitem; private system.windows.forms.toolstripmenuitem savetoolstripmenuitem; private system.windows.forms.toolstripmenuitem opentoolstripmenuitem; private system.windows.forms.toolstripmenuitem closetoolstripmenuitem; private system.windows.forms.openfiledialog openfiledialog1; private system.windows.forms.datagridview grid; private system.windows.forms.datagridviewtextboxcolumn column1; private system.windows.forms.datagridviewtextboxcolumn column2; private system.windows.forms.datagridviewtextboxcolumn column3; private system.windows.forms.datagridviewtextboxcolumn column4; private system.windows.forms.savefiledialog savefiledialog1; } }
program.cs
using system; using system.windows.forms; using testowa; namespace windowsformsapplication1 { static class program { /// <summary> /// main entry point application. /// </summary> [stathread] static void main() { application.enablevisualstyles(); application.setcompatibletextrenderingdefault(false); application.run(new form1()); } } }
Comments
Post a Comment