本文介绍了C#如何从另一个表单文本框更新Datagridview值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 有一个表单(表单1)带有我的文本框和更新按钮,另一个表单(表单2)带有datagridview。我希望能够通过表单1更新按钮更新表单2。希望有人可以帮助我吗? 到目前为止表格1代码: 公共部分类ChangeReservation :表格 { SqlConnection cn = new SqlConnection(Data Source = .\\SQLEXPRESS; Initial Catalog = NovaHotelReservationBookingDatabase; Integrated Security = True); SqlCommand cmd = new SqlCommand(); public SelectReservation SRChild; public ChangeReservation() { InitializeComponent(); } public void updateGridReservation() { cn.Open(); cmd.CommandText =update Reservation set Guest_ Name ='+ txtGuestName.Text +',Children_No ='+ comboBoxNoChildren.Text +',Adults_No ='+ comboBoxNoAdults.Text +',CheckinDate =' + dateCheckin.Text +',CheckoutDate ='+ dateCheckOut.Text +',NumberOfDays ='+ txtDaysNumber.Text +',Room_No ='+ txtRoomNumber.Text +',RoomType ='+ txtRoomType.Text + ',RoomRate ='+ txtRoomRate.Text +',Total ='+ txtTotal.Text +'where Guest_Name ='+ SRChild.gridReservation.SelectedRows [0] .Cells [4] .Value.ToString() +'; cmd.ExecuteNonQuery(); cn.Close(); / / loadgridGuest(); // cleartxt(); MessageBox.Show(成功更新); } private void btnUpdate_Click_1(object sender,EventArgs e) { updateGridReservation() ; } 表格2代码: 公共部分类SelectReservation:表格 { SqlConnecti on cn = new SqlConnection(Data Source = .\\SQLEXPRESS;初始目录= NovaHotelReservationBookingDatabase; Integrated Security = True); SqlCommand cmd = new SqlCommand(); SqlDataReader dr; 私人字符串ReserveID; 私人字符串GName; 私人字符串NoOfChildren; 私人字符串NoOfAdults; private string DateCheckin; private string DateCheckout; private string NoOfdays; private string RoomNumber; private string TypeOfRoom; private string RateOfRoom; private string TotalCost; private string RmID; private string GuestID; public ChangeReservation CRParent {get; set;} public SelectReservation() { InitializeComponent(); } $ / $ private void SelectReservation_Load(object sender,EventArgs e) { cmd.Connection = cn; loadgridReservation(); } void loadgridReservation() { cn.Open(); cmd.CommandText =select * from Reservation; dr = cmd.ExecuteReader(); gridReservation.Rows.Clear(); if(dr.HasRows) { while(dr.Read()) { int n = gridReservation.Rows.Add(); gridReservation.Rows [n] .Cells [0] .Value = dr [0] .ToString(); gridReservation.Rows [n] .Cells [1] .Value = dr [1] .ToString(); gridReservation.Rows [n] .Cells [2] .Value = d r [2] .ToString(); gridReservation.Rows [n] .Cells [3] .Value = dr [3] .ToString(); gridReservation。行[n]。细胞[4] .Value = dr [4] .ToString(); gridReservation.Rows [n]。细胞[5] .Value = dr [5] .ToString( ); gridReservation.Rows [n] .Cells [6] .Value = dr [6] .ToString(); gridReservation.Rows [n] .Cells [ 7] .Value = dr [7] .ToString(); gridReservation.Rows [n] .Cells [8] .Value = dr [8] .ToString(); gridReservation.Rows [n] .Cells [9] .Value = dr [9] .ToString(); gridReservation.Rows [n] .Cells [10] .Value = dr [ 10] .ToString(); gridReservation.Rows [n] .Cells [11] .Value = dr [11] .ToString(); gridReservation.Rows [ n] .Cells [12] .Value = dr [12] .ToString(); } } cn.Close(); } private void gridReservation_CellClick(object sender,DataGridViewCellEventArgs e) { ReserveID = gridReservation.SelectedRows [0] .Cells [0] .Value.ToString(); RmID = gridReservation.SelectedRows [0] .Cells [1]。 Value.ToString(); GuestID = gridReservation.SelectedRows [0] .Cells [2] .Value.ToString(); GName = gridReservation.SelectedRows [0] .Cells [3] .Value.ToString(); NoOfChildren = gridReservation.SelectedRows [0] .Cells [4] .Value.ToString(); NoOfAdults = gridReservation.SelectedRows [0] .Cells [5] .Value.ToString(); DateCheckin = gridReservation.SelectedRows [0] .Cells [6] .Value.ToString(); DateCheckout = gridReservation.SelectedRows [0] .Cells [7] .Value.ToString(); NoOfdays = gridReservation.SelectedRows [0] .Cell s [8] .Value.ToString(); RoomNumber = gridReservation.SelectedRows [0] .Cells [9] .Value.ToString(); TypeOfRoom = gridReservation .SelectedRows [0] .Cells [10] .Value.ToString(); RateOfRoom = gridReservation.SelectedRows [0] .Cells [11] .Value.ToString(); TotalCost = gridReservation.SelectedRows [0] .Cells [12] .Value.ToString(); } private void btnSubmit_Click(object sender,EventArgs e) { CRParent.GetAndDisplayReservationDetails(ReserveID,RmID,GuestID,GName,NoOfChildren,NoOfAdults,DateCheckin,DateCheckout,NoOfdays,RoomNumber,TypeOfRoom ,RateOfRoom,TotalCost); this.Close(); } private void btnSearchGuest_Click(object sender,EventArgs e) { searchGuestName(); } void searchGuestName() { cn.Open(); cmd.CommandText =select * from Reservation where Guest_Name ='+ txtGuestName.Text +';;; dr = cmd.ExecuteReader(); gridReservation.Rows.Clear(); if(dr.HasRows) { while(dr .Read()) { int n = gridReservation.Rows.Add(); gridReservation.Rows [n] .Cells [ 0] .Value = dr [0] .ToString(); gridReservation.Rows [n] .Cells [1] .Value = dr [1] .ToString(); gridReservation.Rows [n] .Cells [2] .Value = dr [2] .ToString(); gridReservation.Rows [n] .Cells [3] .Value = dr [ 3] .ToString(); gridReservation.Rows [n] .Cells [4] .Value = dr [4] .ToString( ); gridReservation.Rows [n] .Cells [5] .Value = dr [5] .ToString(); gridReservation.Rows [n] .Cells [ 6] .Value = dr [6] .ToString(); gridReservation.Rows [n] .Cells [7] .Value = dr [7] .ToString(); gridReservation.Rows [n] .Cells [8] .Value = dr [8] .ToString(); gridReservation.Rows [n] .Cells [9] .Value = dr [ 9] .ToString(); gridReservation.Rows [n] .Cells [10] .Value = dr [10] .ToString(); gridReservation.Rows [ n] .Cells [11] .Value = dr [11] .ToString(); gridReservation.Rows [n] .Cells [12] .Value = dr [12] .ToString(); } } cn.Close(); }have one form (Form 1) with my text boxes and a update button and another form (Form 2) with the datagridview. I want to be able to update the form 2 by form 1 update button. Hope someone can help me?Form 1 code i have so far: public partial class ChangeReservation : Form { SqlConnection cn = new SqlConnection("Data Source=.\\SQLEXPRESS; Initial Catalog =NovaHotelReservationBookingDatabase;Integrated Security=True"); SqlCommand cmd = new SqlCommand(); public SelectReservation SRChild; public ChangeReservation () { InitializeComponent(); } public void updateGridReservation() { cn.Open(); cmd.CommandText = "update Reservation set Guest_ Name= '" + txtGuestName.Text + "', Children_No='" + comboBoxNoChildren.Text + "', Adults_No='" + comboBoxNoAdults.Text + "', CheckinDate='" + dateCheckin.Text + "',CheckoutDate='" + dateCheckOut.Text + "', NumberOfDays='" + txtDaysNumber.Text + "', Room_No= '" + txtRoomNumber.Text + "', RoomType= '" + txtRoomType.Text + "',RoomRate= '" + txtRoomRate.Text + "', Total= '" + txtTotal.Text + "' where Guest_Name='" + SRChild.gridReservation.SelectedRows[0].Cells[4].Value.ToString() + "'"; cmd.ExecuteNonQuery(); cn.Close(); // loadgridGuest(); // cleartxt(); MessageBox.Show("Successfully Updated"); } private void btnUpdate_Click_1(object sender, EventArgs e) { updateGridReservation(); }Form 2 code: public partial class SelectReservation : Form { SqlConnection cn = new SqlConnection("Data Source=.\\SQLEXPRESS; Initial Catalog =NovaHotelReservationBookingDatabase;Integrated Security=True"); SqlCommand cmd = new SqlCommand(); SqlDataReader dr; private string ReserveID; private string GName; private string NoOfChildren; private string NoOfAdults; private string DateCheckin; private string DateCheckout; private string NoOfdays; private string RoomNumber; private string TypeOfRoom; private string RateOfRoom; private string TotalCost; private string RmID; private string GuestID; public ChangeReservation CRParent { get; set; } public SelectReservation() { InitializeComponent(); } private void SelectReservation_Load(object sender, EventArgs e) { cmd.Connection = cn; loadgridReservation(); } void loadgridReservation() { cn.Open(); cmd.CommandText = "select * from Reservation"; dr = cmd.ExecuteReader(); gridReservation.Rows.Clear(); if (dr.HasRows) { while (dr.Read()) { int n = gridReservation.Rows.Add(); gridReservation.Rows[n].Cells[0].Value = dr[0].ToString(); gridReservation.Rows[n].Cells[1].Value = dr[1].ToString(); gridReservation.Rows[n].Cells[2].Value = dr[2].ToString(); gridReservation.Rows[n].Cells[3].Value = dr[3].ToString(); gridReservation.Rows[n].Cells[4].Value = dr[4].ToString(); gridReservation.Rows[n].Cells[5].Value = dr[5].ToString(); gridReservation.Rows[n].Cells[6].Value = dr[6].ToString(); gridReservation.Rows[n].Cells[7].Value = dr[7].ToString(); gridReservation.Rows[n].Cells[8].Value = dr[8].ToString(); gridReservation.Rows[n].Cells[9].Value = dr[9].ToString(); gridReservation.Rows[n].Cells[10].Value = dr[10].ToString(); gridReservation.Rows[n].Cells[11].Value = dr[11].ToString(); gridReservation.Rows[n].Cells[12].Value = dr[12].ToString(); } } cn.Close(); } private void gridReservation_CellClick(object sender, DataGridViewCellEventArgs e) { ReserveID = gridReservation.SelectedRows[0].Cells[0].Value.ToString(); RmID = gridReservation.SelectedRows[0].Cells[1].Value.ToString(); GuestID = gridReservation.SelectedRows[0].Cells[2].Value.ToString(); GName = gridReservation.SelectedRows[0].Cells[3].Value.ToString(); NoOfChildren = gridReservation.SelectedRows[0].Cells[4].Value.ToString(); NoOfAdults = gridReservation.SelectedRows[0].Cells[5].Value.ToString(); DateCheckin = gridReservation.SelectedRows[0].Cells[6].Value.ToString(); DateCheckout = gridReservation.SelectedRows[0].Cells[7].Value.ToString(); NoOfdays = gridReservation.SelectedRows[0].Cells[8].Value.ToString(); RoomNumber = gridReservation.SelectedRows[0].Cells[9].Value.ToString(); TypeOfRoom = gridReservation.SelectedRows[0].Cells[10].Value.ToString(); RateOfRoom = gridReservation.SelectedRows[0].Cells[11].Value.ToString(); TotalCost = gridReservation.SelectedRows[0].Cells[12].Value.ToString(); } private void btnSubmit_Click(object sender, EventArgs e) { CRParent.GetAndDisplayReservationDetails(ReserveID, RmID, GuestID, GName, NoOfChildren, NoOfAdults, DateCheckin, DateCheckout, NoOfdays, RoomNumber, TypeOfRoom, RateOfRoom, TotalCost); this.Close(); } private void btnSearchGuest_Click(object sender, EventArgs e) { searchGuestName(); } void searchGuestName() { cn.Open(); cmd.CommandText = "select * from Reservation where Guest_Name = '" + txtGuestName.Text + "';"; dr = cmd.ExecuteReader(); gridReservation.Rows.Clear(); if (dr.HasRows) { while (dr.Read()) { int n = gridReservation.Rows.Add(); gridReservation.Rows[n].Cells[0].Value = dr[0].ToString(); gridReservation.Rows[n].Cells[1].Value = dr[1].ToString(); gridReservation.Rows[n].Cells[2].Value = dr[2].ToString(); gridReservation.Rows[n].Cells[3].Value = dr[3].ToString(); gridReservation.Rows[n].Cells[4].Value = dr[4].ToString(); gridReservation.Rows[n].Cells[5].Value = dr[5].ToString(); gridReservation.Rows[n].Cells[6].Value = dr[6].ToString(); gridReservation.Rows[n].Cells[7].Value = dr[7].ToString(); gridReservation.Rows[n].Cells[8].Value = dr[8].ToString(); gridReservation.Rows[n].Cells[9].Value = dr[9].ToString(); gridReservation.Rows[n].Cells[10].Value = dr[10].ToString(); gridReservation.Rows[n].Cells[11].Value = dr[11].ToString(); gridReservation.Rows[n].Cells[12].Value = dr[12].ToString(); } } cn.Close(); }推荐答案首先,请停止这样做!不要连接字符串以构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。请改用参数化查询。 其次,你如何做到这一点取决于表格之间的关系: 在两种表格之间转移信息,第1部分:父母对孩子 [ ^ ] 在两个表格之间传递信息,第2部分:儿童到家长 [ ^ ] 在两个表格之间传递信息,第3部分:儿童到孩子 [ ^ ] 第三,你的代码不起作用:您需要将表单实例分配给 SRChild 并调用Show或ShowDialog以显示第二个表单。如果你不创建实例,你将获得一个空引用异常。 你能给我一些关于如何正确操作的例子,因为我是c#的新手吗?我不希望第二种形式在我从表格1更新datagridview后再次显示 尝试:For starters, stop doing that! Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.Secondly, how you do this depends on the "relationship" between the forms:Transferring information between two forms, Part 1: Parent to Child[^]Transferring information between two forms, Part 2: Child to Parent[^]Transferring information between two forms, Part 3: Child to Child[^]And thirdly your code won't work: You need to assign an instance of your form to SRChild and call either Show or ShowDialog to display the second form. If you don't create the instance, you will just get a null reference exception."can you give me example then on how to do properly as i am new to c# please? and i don't want the second form to show again once i updated datagridview from form 1"Try:using (SqlConnection con = new SqlConnection(strConnect)) { con.Open(); using (SqlCommand cmd = new SqlCommand("SELECT Id, description FROM myTable WHERE UserName=@NM", con)) { cmd.Parameters.AddWithValue("@NM", tbUserName.Text); using (SqlDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { int id = (int) reader["Id"]; string desc = (string) reader["description"]; Console.WriteLine("ID: {0}\n {1}", id, desc); } } } } 有关如何执行多种形式的示例,请拥有看看这些链接 - 它们展示了通信并有一个示例应用程序。For an example of how to do multiform stuff, have a look at the links - they demonstrate the communications and have an example app. 这篇关于C#如何从另一个表单文本框更新Datagridview值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-30 08:12