●Form1
public Form1()
{
InitializeComponent();
//콤보 박스에 선택사항 추가하는구문
this.comboBox1.Items.AddRange(new object[] { "남자", "여자","남","여" });
comboBox1.SelectedIndex = 0;
dataGridView1.ReadOnly = true; // datagridview 에 들어가있는 값을 수정불가로 만드는 구문
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; // 넓이를 자동으로 칸이랑 맞춰주는 구문
dataGridView1.AllowUserToAddRows = false; // 마지막 빈칸을 없애는 구문
select();
cleanControl();
}
combobox에 들어갈 값을 만들어줬다.
dataGridView가 실행했을 때 클릭으로 수정할 수 없게 만들었다.
dataGridView의 설정한 넓이랑 출력했을 때 의 넓이랑 똑같이 만들어 줘라.
dataGridView 가 출력했을때 마지막 빈칸을 없애는 구문
●함수호출
select();
cleanControl();
함수로 만들어서 호출 할 수 있게 만들었다.
●select(함수 호출)
private void select(string searchText="") // searchText같이 없으면 빈 공백
{
MessageBox.Show(searchText);
CRUD.con.Open();
string sql = " select * from tb_smart_crud " +
" where concat(firstname,lastname) " +
" like :searchText " +
" order by autoid DESC";
string search = $"%{searchText}%";
CRUD.cmd = new OracleCommand(sql, CRUD.con);
CRUD.cmd.Parameters.Add(":searchText", search);
OracleDataAdapter adapter = new OracleDataAdapter();
adapter.SelectCommand = CRUD.cmd;
DataSet dataSet = new DataSet();
adapter.Fill(dataSet);
dataGridView1.DataSource = dataSet.Tables[0];
CRUD.con.Close();
cleanControl();
if (dataGridView1.Rows.Count > 0)
{
this.autoid = int.Parse(dataGridView1.Rows[0].Cells[0].Value.ToString());
}
}
sql구문에서 select를 한것이고
searchText에 값을 넣어 그값을 찾을 수 있게 하였다.
select구문을 하면
dataGridView1을 통해 보여줘라 하는 구문을 추가 하였다.
dataGridView1 행의 갯수가 0개보다 많으면
첫번째 행과 영을 autoid에 넣어라 라는 말이다.
●cleanControl(함수 호출)
private void cleanControl()
{
FirstNameTextBox.Text = string.Empty;
LastNameTextBox.Text = string.Empty;
button2.Text = "UPDATE";
button3.Text = "DELETE";
}
update나 delete를 하고 나면 firstname과 lastname에 textbox을 빈칸으로 만들었다.
●select 버튼을 클릭했을때
private void select(object sender, EventArgs e)
{
if(SearchText.Text.Equals(""))
select();
else
select(SearchText.Text);
}
select버튼을 누를때 SearchText.Text.Equals가 빈칸이면 select를하고
아니면 SearchText.Text를 보여줘라
●dataGridView1_CellClick
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
this.autoid = int.Parse(dataGridView1.CurrentRow.Cells[0].Value.ToString());
string firstname = dataGridView1.CurrentRow.Cells[1].Value.ToString();
string lastname = dataGridView1.CurrentRow.Cells[2].Value.ToString();
string gender = dataGridView1.CurrentRow.Cells[3].Value.ToString();
FirstNameTextBox.Text = firstname;
LastNameTextBox.Text = lastname;
button2.Text = $"UPDATE ({autoid})";
button3.Text = $"UPDATE ({autoid})";
comboBox1.SelectedItem = gender;
}
dataGridView1의 Cell를 클릭하게 되면
firstname은 firstname의 textbox.Text에 들어가게 해라
lastname은 lastname의 textbox.Text에 들어가게 해라
cell을 클릭했을때 button2.Text는 update의 클릭한 autoid 를 나타내라
button3.Text는 update의 클릭한 autoid 를 나타내라
gender는 cell을 클릭했을때의 gender를 combobox에 나타내라
●UPDATE
private void update(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(FirstNameTextBox.Text.Trim()) || string.IsNullOrEmpty(LastNameTextBox.Text.Trim()))
{
MessageBox.Show("이름을 입력하세요", "경고", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
return;
}
string gender = "남자";
if (comboBox1.SelectedItem != null)
{
gender = comboBox1.SelectedItem.ToString();
}
// 원하는 값을 입력받아서 입력받은값을 보여줘라
string sql = $"update tb_smart_crud" +
$" set firstname = :firstname ," +
$" lastname = :lastname ," +
$" gender = :gender" +
$" where autoid= :autoid";
CRUD.con.Open();
CRUD.cmd = new OracleCommand(sql, CRUD.con);
CRUD.cmd.Parameters.Clear();
CRUD.cmd.Parameters.Add(":firstname", FirstNameTextBox.Text);
CRUD.cmd.Parameters.Add(":lastname", LastNameTextBox.Text);
CRUD.cmd.Parameters.Add(":gender", gender);
CRUD.cmd.Parameters.Add(":autoid", this.autoid);
CRUD.cmd.ExecuteNonQuery();
CRUD.con.Close();
MessageBox.Show("수정되었습니다.");
select();
}
firstnameTextBox와 lastnameTextBox가 빈칸이면 경고문을 출력하게했다.
sql문으로 사용자가 지정한 값으로 수정할 수 있게 만들었다.
마지막에 함수명인 select();를 나둬 자동으로 select를 할 수 있게 했다.
●DELETE
private void delete(object sender, EventArgs e)
{
if (autoid ==0)
{
MessageBox.Show("삭제하는 행을 선택해주세요");
return;
}
string gender = "남자";
if (comboBox1.SelectedItem != null)
{
gender = comboBox1.SelectedItem.ToString();
}
// 원하는 값을 입력받아서 입력받은값을 보여줘라
string sql = $"delete tb_smart_crud where autoid= :autoid";
CRUD.con.Open();
CRUD.cmd = new OracleCommand(sql, CRUD.con);
CRUD.cmd.Parameters.Clear();
CRUD.cmd.Parameters.Add(":autoid", this.autoid);
CRUD.cmd.ExecuteNonQuery();
CRUD.con.Close();
MessageBox.Show("삭제되었습니다..");
select();
}
autoid가 0이면 삭제할 행을 선택하라 라는 문구가 출력되게 하였다.
sql문으로 삭제할 행을 사용자가 지정한 autoid를 삭제할 수 있게끔 하였다.
마지막에 함수명인 select();를 나둬 이구문이 끝나면 자동으로 select하게 만들었다.
'C#' 카테고리의 다른 글
C# 할일 관리 프로그램(2) 사용자관리(insert/select) (0) | 2024.02.28 |
---|---|
c# 할일 관리 프로그램(1) (0) | 2024.02.27 |
C# crud(create, read(select), update, delete) (0) | 2024.02.23 |
label 에 대한 동적 이벤트 (0) | 2024.02.22 |
C# 수업중 내용 최종 (입출력) (0) | 2024.02.21 |