C# - DataGridView - Cannot Get ColumnCount, Cannot Get Cell Values -
based on how dgv being used, cannot use binding data. i'll post being done, in pretty order trying it. problem when try hit save, method acts if has no idea "datagridview1" character is, if it's out of scope, it's not out of scope.
private void refreshdgv1(){ datagridview1.datasource = null; datagridview1.columns.clear(); datagridview1.rows.clear(); string query = (@" select hld_id 'hld_id' , holdname 'hold name' , begindate 'begin date' , filenumber 'file number' , operation 'operation' , brand 'brand' , paddress 'property address' , found 'found' , match 'address match' , secured 'file secured' , relocated 'file relocated' , comment 'comment' records "); //datagridview1.datasource = bindingsource1; //datagridview1.columncount = 11; //datagridview1.columns[0].name = "hold name"; datagridviewtextboxcolumn holdname = new datagridviewtextboxcolumn(); holdname.headertext = "hold name"; holdname.name = "hold name"; holdname.autosizemode = datagridviewautosizecolumnmode.fill; datagridview1.columns.add(holdname); //datagridview1.columns[1].name = "begin date"; datagridviewtextboxcolumn begindate = new datagridviewtextboxcolumn(); begindate.headertext = "begin date"; begindate.name = "begin date"; begindate.autosizemode = datagridviewautosizecolumnmode.fill; datagridview1.columns.add(begindate); //datagridview1.columns[2].name = "file number"; datagridviewtextboxcolumn filenumber = new datagridviewtextboxcolumn(); filenumber.headertext = "file number"; filenumber.name = "file number"; filenumber.autosizemode = datagridviewautosizecolumnmode.fill; datagridview1.columns.add(filenumber); //datagridview1.columns[3].name = "operation"; datagridviewtextboxcolumn operation = new datagridviewtextboxcolumn(); operation.headertext = "operation"; operation.name = "operation"; operation.autosizemode = datagridviewautosizecolumnmode.fill; datagridview1.columns.add(operation); //datagridview1.columns[4].name = "brand"; datagridviewtextboxcolumn brand = new datagridviewtextboxcolumn(); brand.headertext = "brand"; brand.name = "brand"; brand.autosizemode = datagridviewautosizecolumnmode.fill; datagridview1.columns.add(brand); //datagridview1.columns[5].name = "property address"; datagridviewtextboxcolumn propertyaddress = new datagridviewtextboxcolumn(); propertyaddress.headertext = "property address"; propertyaddress.name = "propertyaddress"; propertyaddress.autosizemode = datagridviewautosizecolumnmode.fill; datagridview1.columns.add(propertyaddress); //datagridview1.columns[6].name = "found"; datagridviewcomboboxcolumn found = new datagridviewcomboboxcolumn(); found.headertext = "found"; found.name = "found"; found.items.add(""); found.items.add("found"); found.items.add("not found"); found.items.add("in progress"); found.autosizemode = datagridviewautosizecolumnmode.fill; datagridview1.columns.add(found); //datagridview1.columns[7].name = "address match"; datagridviewcomboboxcolumn addressmatch = new datagridviewcomboboxcolumn(); addressmatch.headertext = "address match"; addressmatch.name = "address match"; addressmatch.items.add(""); addressmatch.items.add("yes"); addressmatch.items.add("no"); addressmatch.autosizemode = datagridviewautosizecolumnmode.fill; datagridview1.columns.add(addressmatch); //datagridview1.columns[8].name = "file secured"; datagridviewcheckboxcolumn filesecured = new datagridviewcheckboxcolumn(); filesecured.headertext = "file secured"; filesecured.name = "file secured"; filesecured.autosizemode = datagridviewautosizecolumnmode.fill; datagridview1.columns.add(filesecured); //datagridview1.columns[9].name = "file relocated"; datagridviewtextboxcolumn filerelocated = new datagridviewtextboxcolumn(); filerelocated.headertext = "file relocated"; filerelocated.name = "file relocated"; filerelocated.autosizemode = datagridviewautosizecolumnmode.fill; datagridview1.columns.add(filerelocated); //datagridview1.columns[10].name = "comment"; datagridviewtextboxcolumn comment = new datagridviewtextboxcolumn(); comment.headertext = "comment"; comment.name = "comment"; comment.autosizemode = datagridviewautosizecolumnmode.fill; comment.width = (datagridview1.width / 11); datagridview1.columns.add(comment); //datagridview1.autosizecolumnsmode = datagridviewautosizecolumnsmode.fill; //other stuff //datagridview1.columns[(datagridview1.columncount-1)].autosizemode = datagridviewautosizecolumnmode.displayedcells; datagridview1.columns[(datagridview1.columncount-1)].defaultcellstyle.wrapmode = datagridviewtristate.true; datagridview1.cellvaluechanged += handler_datagridview1_cellvaluechanged; readsql(query, datagridview1); } private void readsql(string query, datagridview grid){ try{ string connectionstring = "server=servername;database=databasename;persist security info=true;user id=username;password=password"; dataadapter = new sqldataadapter(query, connectionstring); sqlcommandbuilder commandbuilder = new sqlcommandbuilder(dataadapter); datatable table = new datatable(); table.locale = system.globalization.cultureinfo.invariantculture; dataadapter.fill(table); for(int = 0 ; < table.rows.count; i++){ int n = grid.rows.add(); grid.columncount = 11; initializecomponent(); //add id array (list) recordids.add(table.rows[i].itemarray[0].tostring()); grid.rows[n].cells[0].value = table.rows[i].itemarray[1].tostring(); grid.rows[n].cells[1].value = table.rows[i].itemarray[2].tostring(); grid.rows[n].cells[2].value = table.rows[i].itemarray[3].tostring(); grid.rows[n].cells[3].value = table.rows[i].itemarray[4].tostring(); grid.rows[n].cells[4].value = table.rows[i].itemarray[5].tostring(); grid.rows[n].cells[5].value = table.rows[i].itemarray[6].tostring(); string selection1 = table.rows[i].itemarray[7].tostring(); switch(selection1){ case "found" : try{grid.rows[n].cells[6].value = (grid.rows[i].cells[6] datagridviewcomboboxcell).items[1];}catch{}; break; case "not found" : try{grid.rows[n].cells[6].value = (grid.rows[i].cells[6] datagridviewcomboboxcell).items[2];}catch{}; break; case "in progress" : try{grid.rows[n].cells[6].value = (grid.rows[i].cells[6] datagridviewcomboboxcell).items[3];}catch{}; break; default : try{grid.rows[n].cells[6].value = (grid.rows[i].cells[6] datagridviewcomboboxcell).items[0];}catch{}; break; } string selection2 = table.rows[i].itemarray[8].tostring(); switch(selection2){ case "yes" : try{grid.rows[n].cells[7].value = (grid.rows[i].cells[7] datagridviewcomboboxcell).items[1];}catch{}; break; case "no" : try{grid.rows[n].cells[7].value = (grid.rows[i].cells[7] datagridviewcomboboxcell).items[2];}catch{}; break; default : try{grid.rows[n].cells[7].value = (grid.rows[i].cells[7] datagridviewcomboboxcell).items[0];}catch{}; break; } try{grid.rows[n].cells[8].value = table.rows[i].itemarray[9].tostring();}catch{} try{grid.rows[n].cells[9].value = table.rows[i].itemarray[10].tostring();}catch{} try{grid.rows[n].cells[10].value = table.rows[i].itemarray[11].tostring();}catch{} } loaddgv1tooltips(); grid.autoresizecolumns(datagridviewautosizecolumnsmode.allcellsexceptheader); grid.rowheadersvisible = false; grid.enableheadersvisualstyles = false; grid.columnheadersdefaultcellstyle.backcolor = color.dimgray; grid.columnheadersdefaultcellstyle.forecolor = color.white; grid.gridcolor = color.royalblue; for(int = 0 ; < grid.columns.count; i++){ grid.columns[i].width = (grid.size.width / grid.columns.count) - 1; } }catch(sqlexception ex){ messagebox.show("sql error: " + ex.tostring()); messagebox.show(query); } } private void button4_click(object sender, eventargs e){ //save string name = ""; string date = ""; string file = ""; string operation = ""; string brand = ""; string address = ""; string found = ""; string match = ""; string secured = ""; string relocated = ""; string comment = ""; console.writeline("column count: " + this.datagridview1.columncount); (int = 0; < this.datagridview1.columncount; i++){ switch(i){ //put handlers in here null values, try/catch? case 0: try{name = this.datagridview1.selectedcells[0].owningrow.cells[i].value.tostring();}catch{}; break; case 1: try{date = this.datagridview1.selectedcells[0].owningrow.cells[i].value.tostring();}catch{}; break; case 2: try{file = this.datagridview1.selectedcells[0].owningrow.cells[i].value.tostring();}catch{}; break; case 3: try{operation = this.datagridview1.selectedcells[0].owningrow.cells[i].value.tostring();}catch{}; break; case 4: try{brand = this.datagridview1.selectedcells[0].owningrow.cells[i].value.tostring();}catch{}; break; case 5: try{address = this.datagridview1.selectedcells[0].owningrow.cells[i].value.tostring();}catch{}; break; case 6: try{found = this.datagridview1.selectedcells[0].owningrow.cells[i].value.tostring();}catch{}; break; case 7: try{match = this.datagridview1.selectedcells[0].owningrow.cells[i].value.tostring();}catch{}; break; case 8: try{secured = this.datagridview1.selectedcells[0].owningrow.cells[i].value.tostring();}catch{}; break; case 9: try{relocated = this.datagridview1.selectedcells[0].owningrow.cells[i].value.tostring();}catch{}; break; case 10: try{comment = this.datagridview1.selectedcells[0].owningrow.cells[i].value.tostring();}catch{}; break; default: break; //do nothing. } } if(secured != "true"){secured = "false";} string query = (@" update records set holdname = '" + name + "', begindate = '" + date + "', filenumber = '" + file + "', operation = '" + operation + "', brand = '" + brand + "', paddress = '" + address + "', found = '" + found + "', match = '" + match + "', secured = '" + secured + "', relocated = '" + relocated + "', comment = '" + comment + "'" + "where hld_id = '" + hld_id + "'"); //writesql(query); console.writeline("query: " + query); refreshdgv1(); }
here static class use make calling stored procedures better perhaps can utilize learn
//if want return dataset public static class sqldbhelper { public static dataset executedataset(string sql, commandtype cmdtype, params sqlparameter[] parameters) { using (dataset ds = new dataset()) using (sqlconnection connstr = new sqlconnection(configurationmanager.connectionstrings["dbconn"].connectionstring)) using (sqlcommand cmd = new sqlcommand(sql, connstr)) { cmd.commandtype = cmdtype; foreach (var item in parameters) { cmd.parameters.add(item); } try { cmd.connection.open(); new sqldataadapter(cmd).fill(ds); } catch (exception ex) { //throw; trap exception(s) here } return ds; } } //if want return datatable public static datatable executedataset(string sql, commandtype cmdtype, params sqlparameter[] parameters) { using (dataset ds = new dataset()) using (sqlconnection connstr = new sqlconnection(configurationmanager.connectionstrings["dbconn"].connectionstring)) using (sqlcommand cmd = new sqlcommand(sql, connstr)) { cmd.commandtype = cmdtype; foreach (var item in parameters) { cmd.parameters.add(item); } try { cmd.connection.open(); new sqldataadapter(cmd).fill(ds); } catch (exception ex) { //show message or log message on ex.message } return ds.tables[0]; } } }
Comments
Post a Comment