c# - Datagridview column not hide -
i have problem @ datagridview hide column..
i don't know why code doesn't work..
this code..
private void kodesatuanlist() // 1 { connection.sqlconnection.close(); connection.connector(server, database, user, password); adapterkodesatuan = new sqldataadapter( "select id,kosat,keterangan kosat", connection.sqlconnection); datatablekodesatuan.clear(); datagridview1.clearselection(); adapterkodesatuan.fill(datatablekodesatuan); dataviewkodesatuan = datatablekodesatuan.defaultview; datagridview1.columncount = 3; datagridview1.columns[0].visible = false; //this..column id, set false.. datagridview1.columns[0].name = "id"; datagridview1.columns[0].headertext = "id"; datagridview1.columns[0].datapropertyname = "id"; datagridview1.columns[1].name = "kode"; datagridview1.columns[1].headertext = "kode"; datagridview1.columns[1].datapropertyname = "kosat"; datagridview1.columns[2].headertext = "keterangan"; datagridview1.columns[2].name = "keterangan"; datagridview1.columns[2].datapropertyname = "keterangan"; datagridview1.columns[1].sortmode = datagridviewcolumnsortmode.automatic; datagridview1.columns[2].sortmode = datagridviewcolumnsortmode.automatic; datagridview1.datasource = dataviewkodesatuan; console.writeline("datagridview1.rows.count = " + datagridview1.rows.count); datagridview1.rows[datagridview1.rows.count - 1].selected = true; }
according code...when run code...column "id" must invisible.
please watch this video.. recorded ... or video 4shared.
i still have no idea problem , still dont know whats wrong code... beside that... change code this..but imnot using sqldataadapter..
private void kodesatuanlist()//this 1 { connection.sqlconnection.close(); connection.connector(server, database, user, password); connection.sqlcommand.connection = connection.sqlconnection; connection.sqlcommand.commandtype = commandtype.text; connection.sqlcommand.commandtext = "select id,kosat,keterangan kosat"; sqldatareader kosatlist = connection.sqlcommand.executereader(); while (kosatlist.read()) { datagridview1.rows.add(kosatlist["id"], kosatlist["kosat"], kosatlist["keterangan"]); } connection.sqlconnection.close(); //adapterkodesatuan = new sqldataadapter("select id,kode,keterangan kosat", connection.sqlconnection); //datatablekodesatuan.clear(); //datagridview1.columns.clear(); ////datagridview1.clearselection(); //datagridview1.columncount = 3; ////datagridview1.columns[0].visible = false; //this..column id, set false.. //datagridview1.columns[0].name = "id"; //datagridview1.columns[0].headertext = "id"; //datagridview1.columns[0].datapropertyname = "id"; //datagridview1.columns[1].name = "kode"; //datagridview1.columns[1].headertext = "kode"; //datagridview1.columns[1].datapropertyname = "kosat"; //datagridview1.columns[2].headertext = "keterangan"; //datagridview1.columns[2].name = "keterangan"; //datagridview1.columns[2].datapropertyname = "keterangan"; //datagridview1.columns[1].sortmode = datagridviewcolumnsortmode.automatic; //datagridview1.columns[2].sortmode = datagridviewcolumnsortmode.automatic; //adapterkodesatuan.fill(datatablekodesatuan); //dataviewkodesatuan = datatablekodesatuan.defaultview; //datagridview1.datasource = dataviewkodesatuan; ////datagridview1.columns[0].visible = false; //console.writeline("datagridview1.rows.count = " + datagridview1.rows.count); //datagridview1.rows[datagridview1.rows.count - 1].selected = true; //this.datagridview1.columns[0].visible = false; //try //{ // connection.connector(server, database, user, password); // listview1.columns.add("kode", 70); // listview1.columns.add("keterangan", 140); // //listview1.items.clear(); // adapterkodesatuan = new sqldataadapter("select * kosat deleteflag='n' ", connection.sqlconnection); // datatable dt = new datatable(); // adapterkodesatuan.fill(dt); // listviewitem listitem; // console.writeline("size = " + dt.rows.count); // (int = 0; < dt.rows.count; i++) // { // datarow dr = dt.rows[i]; // listitem = new listviewitem(); // listitem.subitems.add( dr[0].tostring() ); // listitem.subitems.add( dr[1].tostring() ); // listitem.subitems.add( dr[2].tostring() ); // console.writeline("aaa = " + dr[0].tostring()); // console.writeline("bbb = " + dr[1].tostring()); // console.writeline("ccc = " + dr[2].tostring()); // listview1.items.add(listitem); // } // listview1.show(); //} //catch (exception ms) //{ // console.writeline("aaa"); //} }
thi code in kodesatuan.cs
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.threading.tasks; using system.windows.forms; using microsoft.win32; using system.data.sqlclient; namespace belajarvariabelglobal { public partial class kodesatuan : form { public readonly static string server = registry.localmachine.opensubkey("software\\session", true).getvalue("server").tostring(); public readonly static string database = registry.localmachine.opensubkey("software\\session", true).getvalue("database").tostring(); public readonly static string user = registry.localmachine.opensubkey("software\\session", true).getvalue("user").tostring(); public readonly static string password = registry.localmachine.opensubkey("software\\session", true).getvalue("pass").tostring(); public static string kosat; public static string ket; sqldatareader reader; sqldataadapter adapterkodesatuan; datatable datatablekodesatuan = new datatable(); dataview dataviewkodesatuan = new dataview(); public kodesatuan() { initializecomponent(); } private void kodesatuanlist()//this 1 { connection.sqlconnection.close(); connection.connector(server, database, user, password); connection.sqlcommand.connection = connection.sqlconnection; connection.sqlcommand.commandtype = commandtype.text; connection.sqlcommand.commandtext = "select id,kosat,keterangan kosat"; sqldatareader kosatlist = connection.sqlcommand.executereader(); while (kosatlist.read()) { datagridview1.rows.add(kosatlist["id"], kosatlist["kosat"], kosatlist["keterangan"]); } connection.sqlconnection.close(); //adapterkodesatuan = new sqldataadapter("select id,kode,keterangan kosat", connection.sqlconnection); //datatablekodesatuan.clear(); //datagridview1.columns.clear(); ////datagridview1.clearselection(); //datagridview1.columncount = 3; ////datagridview1.columns[0].visible = false; //this..column id, set false.. //datagridview1.columns[0].name = "id"; //datagridview1.columns[0].headertext = "id"; //datagridview1.columns[0].datapropertyname = "id"; //datagridview1.columns[1].name = "kode"; //datagridview1.columns[1].headertext = "kode"; //datagridview1.columns[1].datapropertyname = "kosat"; //datagridview1.columns[2].headertext = "keterangan"; //datagridview1.columns[2].name = "keterangan"; //datagridview1.columns[2].datapropertyname = "keterangan"; //datagridview1.columns[1].sortmode = datagridviewcolumnsortmode.automatic; //datagridview1.columns[2].sortmode = datagridviewcolumnsortmode.automatic; //adapterkodesatuan.fill(datatablekodesatuan); //dataviewkodesatuan = datatablekodesatuan.defaultview; //datagridview1.datasource = dataviewkodesatuan; ////datagridview1.columns[0].visible = false; //console.writeline("datagridview1.rows.count = " + datagridview1.rows.count); //datagridview1.rows[datagridview1.rows.count - 1].selected = true; //this.datagridview1.columns[0].visible = false; //try //{ // connection.connector(server, database, user, password); // listview1.columns.add("kode", 70); // listview1.columns.add("keterangan", 140); // //listview1.items.clear(); // adapterkodesatuan = new sqldataadapter("select * kosat deleteflag='n' ", connection.sqlconnection); // datatable dt = new datatable(); // adapterkodesatuan.fill(dt); // listviewitem listitem; // console.writeline("size = " + dt.rows.count); // (int = 0; < dt.rows.count; i++) // { // datarow dr = dt.rows[i]; // listitem = new listviewitem(); // listitem.subitems.add( dr[0].tostring() ); // listitem.subitems.add( dr[1].tostring() ); // listitem.subitems.add( dr[2].tostring() ); // console.writeline("aaa = " + dr[0].tostring()); // console.writeline("bbb = " + dr[1].tostring()); // console.writeline("ccc = " + dr[2].tostring()); // listview1.items.add(listitem); // } // listview1.show(); //} //catch (exception ms) //{ // console.writeline("aaa"); //} } private void toolstripbutton2_click(object sender, eventargs e) { toolstripbutton1.enabled = false; toolstripbutton2.enabled = false; toolstripbutton3.enabled = false; toolstripbutton4.visible = false; toolstripbutton5.visible = false; toolstripbutton6.visible = false; toolstripbutton7.visible = false; toolstripbutton10.visible = true; toolstripbutton11.visible = true; kodesatuantxt.enabled = true; keterangantxt.enabled = true; deleteflag.enabled = true; } private void kodesatuan_load(object sender, eventargs e) { kodesatuantxt.enabled = false; keterangantxt.enabled = false; deleteflag.enabled = false; toolstripbutton2.visible = false; toolstripbutton3.visible = false; toolstripbutton10.visible = false; toolstripbutton11.visible = false; //kodesatuanlist();//this function call datagridview or list } private void toolstripbutton11_click(object sender, eventargs e) { dialogresult batal = messagebox.show("perubahan dibatalkan ?", "confirm", messageboxbuttons.yesno, messageboxicon.question); if (batal == dialogresult.yes) { //this.close(); kodesatuantxt.text = kosat; keterangantxt.text = ket; kodesatuantxt.enabled = false; keterangantxt.enabled = false; deleteflag.enabled = false; toolstripbutton1.enabled = true; toolstripbutton2.enabled = true; toolstripbutton3.enabled = true; toolstripbutton4.visible = true; toolstripbutton5.visible = true; toolstripbutton6.visible = true; toolstripbutton7.visible = true; toolstripbutton8.visible = true; toolstripbutton9.visible = true; toolstripbutton10.visible = false; toolstripbutton11.visible = false; } } private void button1_click(object sender, eventargs e) { this.close(); } private void toolstripbutton1_click(object sender, eventargs e) { kodesatuantxt.enabled = true; keterangantxt.enabled = true; deleteflag.enabled = true; toolstripbutton10.visible = true; toolstripbutton11.visible = true; toolstripbutton1.enabled = false; toolstripbutton2.visible = false; toolstripbutton3.visible = false; toolstripbutton4.enabled = false; toolstripbutton5.enabled = false; toolstripbutton6.enabled = false; toolstripbutton7.enabled = false; toolstripbutton8.enabled = false; toolstripbutton9.enabled = false; kodesatuantxt.text = null; keterangantxt.text = null; idlbl.text = "null"; } private void toolstripbutton10_click(object sender, eventargs e) {//ini setelah saya menekan tombol save...atau simpan try { string nonaktif = "n"; string keterangantmp = null; if (kodesatuantxt.text.equals("")) { messagebox.show("kode satuan tidak boleh kosong!", "kode satuan"); kodesatuantxt.focus(); return; } else { if(deleteflag.checked == true){ nonaktif = "y"; } if(!keterangantxt.text.equals("")){ keterangantmp = keterangantxt.text; } if (idlbl.text.equals("null")) { connection.connector(server, database, user, password); connection.sqlcommand = new sqlcommand("select * kosat kosat = '" + kodesatuantxt.text + "'", connection.sqlconnection); reader = connection.sqlcommand.executereader(); if (reader.read()) { //string id = convert.tostring(reader["id"]); //jika data yang mau saya buat itu belum ada...baru bisa insert baru.. dialogresult avaiable = messagebox.show("kode satuan " + kodesatuantxt.text + " sudah ada", "duplicate", messageboxbuttons.ok, messageboxicon.error); if (avaiable == dialogresult.yes) { this.close(); } return; } else {//ini coding buat insertnya.. connection.sqlconnection.close(); connection.connector(server, database, user, password); connection.sqlcommand = new sqlcommand(); connection.sqlcommand.connection = connection.sqlconnection; connection.sqlcommand.commandtype = commandtype.text; connection.sqlcommand.commandtext = "insert kosat(kosat,keterangan,deleteflag) values('" + kodesatuantxt.text + "','" + keterangantmp + "','" + nonaktif + "')"; //con.open(); connection.sqlcommand.executenonquery(); connection.sqlconnection.close(); kodesatuanlist();//disini saya panggil fungsi tadi.. //datagridview1.rows[datagridview1.rows.count - 1].selected = true; //reader = connection.sqlcommand.executereader(); } } else { connection.connector(server, database, user, password); connection.sqlcommand = new sqlcommand(); connection.sqlcommand.connection = connection.sqlconnection; connection.sqlcommand.commandtype = commandtype.text; connection.sqlcommand.commandtext = "update kosat set kosat = '" + kodesatuantxt.text + "', keterangan = '" + keterangantmp + "', deleteflag = '" + nonaktif + "' id = "+ idlbl.text; //con.open(); console.writeline(connection.sqlcommand.commandtext); connection.sqlcommand.executenonquery(); connection.sqlconnection.close(); kodesatuanlist(); } } } catch { } } private void datagridview1_cellmouseclick(object sender, datagridviewcellmouseeventargs e) { system.text.stringbuilder cellinformation = new system.text.stringbuilder(); cellinformation.appendformat("{0} = {1}", "columnindex", e.columnindex); cellinformation.appendline(); cellinformation.appendformat("{0} = {1}", "rowindex", e.rowindex); cellinformation.appendline(); string row = datagridview1.rows[e.rowindex].tostring(); messagebox.show(datagridview1.rows[e.rowindex].cells[0].value.tostring(), "aa");//berdasarkan coding, kalau di coding 3 row, 1 row hidden brrt jadinya tetap 3 row messagebox.show(datagridview1.selectedcells[0].value.tostring(), "cellmouseclick event");//berdasarkan tampilan, kalau di coding 3 row, 1 row hidden jadinya sisa 2 row //kodesatuantxt.text = datagridview1.rows[e.rowindex].cells[1].value.tostring(); //keterangantxt.text = datagridview1.rows[e.rowindex].cells[2].value.tostring(); ////kodesatuan = datagridview1.rows[e.rowindex].cells[1].value.tostring(); ////keterangan = datagridview1.rows[e.rowindex].cells[2].value.tostring(); //toolstripbutton2.visible = true; //toolstripbutton3.visible = true; } private void datagridview1_columnheadermouseclick(object sender, datagridviewcellmouseeventargs e) { datagridview1.columns[1].sortmode = datagridviewcolumnsortmode.automatic; } private void button2_click(object sender, eventargs e) { datagridview1.columns[1].sortmode = datagridviewcolumnsortmode.automatic; } private void datagridview1_selectionchanged(object sender, eventargs e) { if (datagridview1.selectedcells.count > 0) { int selectedrowindex = datagridview1.selectedcells[0].rowindex; datagridviewrow selectedrow = datagridview1.rows[selectedrowindex]; string x = convert.tostring(selectedrow.cells[0].value); string code = convert.tostring(selectedrow.cells[1].value); string description = convert.tostring(selectedrow.cells[2].value); //string = convert.tostring(selectedrow.cells["id"].value); //id nama dari header table console.writeline("a = "+x); console.writeline("kodee = " + code); console.writeline("ket = " + description); idlbl.text = x; kodesatuantxt.text = code; keterangantxt.text = description; kosat = code; ket = description; toolstripbutton2.visible = true; toolstripbutton3.visible = true; } } }
in circustances verified .visible
property of first column lost when populate datagridview
.
i solved moving column in last position or setting datacolumn.columnmapping mappingtype.hidden
.
in code try changing initialization of adapterkodesatuan
as:
adapterkodesatuan = new sqldataadapter("select kosat, keterangan, id kosat", connection.sqlconnection);
so can modify .visible
property of id
column accessing datagridview1.columns[2]
.
use datacolumn.columnmapping
solution when column useless , don't need in other parts of code.
Comments
Post a Comment