|
|
|
สอบถามเรื่องการเขียน code ซ้ำๆ เข้าคนละ Componentแต่ ข้อแม้เป็นเหมือนกันทุกประการ |
|
|
|
|
|
|
|
รบกวนสอบถามครับมีวิธีอื่นง่ายๆกว่านี้ไหมครับ มี 10 component คือ combobox 10 ตัวต้องการโหลดเข้า combobox ใคร combobox มัน มีวิธีเขียนย่อๆไหมครับ
Code (C#)
dtSection1 = Database.GetData(strconMySql, "SELECT ID,NAME_SECTION FROM SECTION WHERE ISENABLE = '1' ORDER BY NAME_SECTION");
dtSection2 = Database.GetData(strconMySql, "SELECT ID,NAME_SECTION FROM SECTION WHERE ISENABLE = '1' ORDER BY NAME_SECTION");
dtSection3 = Database.GetData(strconMySql, "SELECT ID,NAME_SECTION FROM SECTION WHERE ISENABLE = '1' ORDER BY NAME_SECTION");
dtSection4 = Database.GetData(strconMySql, "SELECT ID,NAME_SECTION FROM SECTION WHERE ISENABLE = '1' ORDER BY NAME_SECTION");
dtSection5 = Database.GetData(strconMySql, "SELECT ID,NAME_SECTION FROM SECTION WHERE ISENABLE = '1' ORDER BY NAME_SECTION");
dtSection6 = Database.GetData(strconMySql, "SELECT ID,NAME_SECTION FROM SECTION WHERE ISENABLE = '1' ORDER BY NAME_SECTION");
dtSection7 = Database.GetData(strconMySql, "SELECT ID,NAME_SECTION FROM SECTION WHERE ISENABLE = '1' ORDER BY NAME_SECTION");
dtSection8 = Database.GetData(strconMySql, "SELECT ID,NAME_SECTION FROM SECTION WHERE ISENABLE = '1' ORDER BY NAME_SECTION");
dtSection9 = Database.GetData(strconMySql, "SELECT ID,NAME_SECTION FROM SECTION WHERE ISENABLE = '1' ORDER BY NAME_SECTION");
dtSection10 = Database.GetData(strconMySql, "SELECT ID,NAME_SECTION FROM SECTION WHERE ISENABLE = '1' ORDER BY NAME_SECTION");
try
{
DataRow dr1 = dtSection1.NewRow();
dr1["NAME_SECTION"] = "โปรดเลือก";
dr1["ID"] = 0;
dtSection1.Rows.InsertAt(dr1, 0);
DataView dv1 = dtSection1.DefaultView;
DataRow dr2 = dtSection2.NewRow();
dr2["NAME_SECTION"] = "โปรดเลือก";
dr2["ID"] = 0;
dtSection2.Rows.InsertAt(dr2, 0);
DataView dv2 = dtSection2.DefaultView;
DataRow dr3 = dtSection3.NewRow();
dr3["NAME_SECTION"] = "โปรดเลือก";
dr3["ID"] = 0;
dtSection3.Rows.InsertAt(dr3, 0);
DataView dv3 = dtSection3.DefaultView;
DataRow dr4 = dtSection4.NewRow();
dr4["NAME_SECTION"] = "โปรดเลือก";
dr4["ID"] = 0;
dtSection4.Rows.InsertAt(dr4, 0);
DataView dv4 = dtSection4.DefaultView;
DataRow dr5 = dtSection5.NewRow();
dr5["NAME_SECTION"] = "โปรดเลือก";
dr5["ID"] = 0;
dtSection5.Rows.InsertAt(dr5, 0);
DataView dv5 = dtSection5.DefaultView;
DataRow dr6 = dtSection6.NewRow();
dr6["NAME_SECTION"] = "โปรดเลือก";
dr6["ID"] = 0;
dtSection6.Rows.InsertAt(dr6, 0);
DataView dv6 = dtSection6.DefaultView;
DataRow dr7 = dtSection7.NewRow();
dr7["NAME_SECTION"] = "โปรดเลือก";
dr7["ID"] = 0;
dtSection7.Rows.InsertAt(dr7, 0);
DataView dv7 = dtSection7.DefaultView;
DataRow dr8 = dtSection8.NewRow();
dr8["NAME_SECTION"] = "โปรดเลือก";
dr8["ID"] = 0;
dtSection8.Rows.InsertAt(dr8, 0);
DataView dv8 = dtSection8.DefaultView;
DataRow dr9 = dtSection9.NewRow();
dr9["NAME_SECTION"] = "โปรดเลือก";
dr9["ID"] = 0;
dtSection9.Rows.InsertAt(dr9, 0);
DataView dv9 = dtSection9.DefaultView;
DataRow dr10 = dtSection10.NewRow();
dr10["NAME_SECTION"] = "โปรดเลือก";
dr10["ID"] = 0;
dtSection10.Rows.InsertAt(dr10, 0);
DataView dv10 = dtSection10.DefaultView;
cboSection1.DataSource = dv1;
cboSection1.DisplayMember = "NAME_SECTION";
cboSection1.ValueMember = "ID";
cboSection2.DataSource = dv2;
cboSection2.DisplayMember = "NAME_SECTION";
cboSection2.ValueMember = "ID";
cboSection3.DataSource = dv3;
cboSection3.DisplayMember = "NAME_SECTION";
cboSection3.ValueMember = "ID";
cboSection4.DataSource = dv4;
cboSection4.DisplayMember = "NAME_SECTION";
cboSection4.ValueMember = "ID";
cboSection5.DataSource = dv5;
cboSection5.DisplayMember = "NAME_SECTION";
cboSection5.ValueMember = "ID";
cboSection6.DataSource = dv6;
cboSection6.DisplayMember = "NAME_SECTION";
cboSection6.ValueMember = "ID";
cboSection7.DataSource = dv7;
cboSection7.DisplayMember = "NAME_SECTION";
cboSection7.ValueMember = "ID";
cboSection8.DataSource = dv8;
cboSection8.DisplayMember = "NAME_SECTION";
cboSection8.ValueMember = "ID";
cboSection9.DataSource = dv9;
cboSection9.DisplayMember = "NAME_SECTION";
cboSection9.ValueMember = "ID";
cboSection10.DataSource = dv10;
cboSection10.DisplayMember = "NAME_SECTION";
cboSection10.ValueMember = "ID";
}
catch
{
MessageBox.Show("ไม่สามามรถนำ Section ขึ้นมาแสดงได้", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
หรือแม้แต่ในกรณีนี้ก็ตามต้องเขียน Code ยาวมาครับทั้งที่ชื่อ component ต่างกันแค่เพียงตัวเลขครับ
Code (C#)
txtTopic2.Visible = false;
txtTopic3.Visible = false;
txtTopic4.Visible = false;
txtTopic5.Visible = false;
txtTopic6.Visible = false;
txtTopic7.Visible = false;
txtTopic8.Visible = false;
txtTopic9.Visible = false;
txtTopic10.Visible = false;
cboSection2.Visible = false;
cboSection3.Visible = false;
cboSection4.Visible = false;
cboSection5.Visible = false;
cboSection6.Visible = false;
cboSection7.Visible = false;
cboSection8.Visible = false;
cboSection9.Visible = false;
cboSection10.Visible = false;
cboRoom2.Visible = false;
cboRoom3.Visible = false;
cboRoom4.Visible = false;
cboRoom5.Visible = false;
cboRoom6.Visible = false;
cboRoom7.Visible = false;
cboRoom8.Visible = false;
cboRoom9.Visible = false;
cboRoom10.Visible = false;
cboStartTime2.Visible = false;
cboStartTime3.Visible = false;
cboStartTime4.Visible = false;
cboStartTime5.Visible = false;
cboStartTime6.Visible = false;
cboStartTime7.Visible = false;
cboStartTime8.Visible = false;
cboStartTime9.Visible = false;
cboStartTime10.Visible = false;
cboEndTime2.Visible = false;
cboEndTime3.Visible = false;
cboEndTime4.Visible = false;
cboEndTime5.Visible = false;
cboEndTime6.Visible = false;
cboEndTime7.Visible = false;
cboEndTime8.Visible = false;
cboEndTime9.Visible = false;
cboEndTime10.Visible = false;
txtTeacher2.Visible = false;
txtTeacher3.Visible = false;
txtTeacher4.Visible = false;
txtTeacher5.Visible = false;
txtTeacher6.Visible = false;
txtTeacher7.Visible = false;
txtTeacher8.Visible = false;
txtTeacher9.Visible = false;
txtTeacher10.Visible = false;
รบกวนขอคำแนะนำด้วยนะครับ ขอบคุณครับ
Tag : .NET, MySQL, C#
|
ประวัติการแก้ไข 2015-05-14 13:50:43
|
|
|
|
|
Date :
2015-05-14 13:49:36 |
By :
teerapat_kan |
View :
651 |
Reply :
2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
สร้างเป็น Function ไว้ซิครับ
Code (C#)
public static void DDLMaster(Combobox ddl)
{
dtSection1 = Database.GetData(strconMySql, "SELECT ID,NAME_SECTION FROM SECTION WHERE ISENABLE = '1' ORDER BY NAME_SECTION");
DataRow dr1 = dtSection1.NewRow();
dr1["NAME_SECTION"] = "โปรดเลือก";
dr1["ID"] = 0;
dtSection1.Rows.InsertAt(dr1, 0);
DataView dv1 = dtSection1.DefaultView;
cboSection1.DataSource = dv1;
cboSection1.DisplayMember = "NAME_SECTION";
cboSection1.ValueMember = "ID";
}
DDLMaster(this.cboSection1);
DDLMaster(this.cboSection2);
DDLMaster(this.cboSection3);
DDLMaster(this.cboSection4);
ประมาณนี้ครับ
|
|
|
|
|
Date :
2015-05-14 16:27:43 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ขอบคุณครับเดี๋ยวลองไปทำดูครับ
|
|
|
|
|
Date :
2015-05-14 17:38:43 |
By :
teerapat_kan |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 04
|