2013-03-27 5 views
-2

나는 2 가지 형식을 가지고 있으며 일부는 단순히 나를 안내 할 수 있다면 datagridview를 업데이트하는 방법을 모른다. form1은 다음과 같습니다 :datagridview 업데이트

public partial class frmBooking : Form 
{ 
    //instance of sqlConnection created   
    SqlConnection con = new SqlConnection("Data Source=...."); 


    public frmBooking() 
    { 
     InitializeComponent(); 

     //sets the time selecter to a time format and selects the hours and mins only in 24 hour format. 
     TimeOfBooking.CustomFormat = "HH:mm"; 
     TimeOfBooking.Format = System.Windows.Forms.DateTimePickerFormat.Custom; 

     //combo box get data from database and updates when new customer is added 
     try 
     { 
      con.Open(); 
     } 
     catch (SqlException ex) 
     { 
      MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error); 
      Application.Exit(); 
     } 

     NameOfCustomer.Items.Clear(); 

     SqlCommand cm = new SqlCommand("SELECT GroupName FROM Customer ORDER BY GroupName ASC", con); 

     try 
     { 
      SqlDataReader dr = cm.ExecuteReader(); 

      while (dr.Read()) 
      { 
       NameOfCustomer.Items.Add(dr["GroupName"]); 
      } 

      dr.Close(); 
      dr.Dispose(); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error); 

     } 


     //ends here 
    } 

     //sets the facility name so its copied from the previous(facility to be booked) form into the Facility text box 
    public void setFacility(string new_facility) 
    { 
     Facility.Text = new_facility; 
    } 

    //sets the date so its copied from the previous(facility to be booked) form into the date text box 
    public void setDate(string new_date) 
    { 
     Date.Text = new_date; 
    } 


    //adding a new customer button, redirects user to the add customer page 
    private void btnAddCust_Click(object sender, EventArgs e) 
    { 
     frmNewCustomer newCust = new frmNewCustomer(); 
     newCust.Show(); 
     this.Close(); 
    } 

    //cancel button will redirect the user to the main page 
    private void btnCancel_Click(object sender, EventArgs e) 
    { 
     Form3 frm3 = new Form3(); 
     frm3.Show(); 
     this.Close(); 
    } 

    private void frmBooking_Load(object sender, EventArgs e) 
    { 
     // TODO: This line of code loads data into the 'usersDataSet.Customer' table. You can move, or remove it, as needed. 
     this.customerTableAdapter.Fill(this.usersDataSet.Customer); 
     // TODO: This line of code loads data into the 'usersDataSet.Customer' table. You can move, or remove it, as needed. 
     this.customerTableAdapter.Fill(this.usersDataSet.Customer); 
     // TODO: This line of code loads data into the 'usersDataSet.Customer' table. You can move, or remove it, as needed. 
     this.customerTableAdapter.Fill(this.usersDataSet.Customer); 

    } 

    private void lblBookingForm_Click(object sender, EventArgs e) 
    { 

    } 

    private void btnConfirm_Click(object sender, EventArgs e) 
    { 

     //validation - if any of the mandotory fields are empty an error message will apear next to the text box 
     if (Facility.Text == "") 
     { 
      //MessageBox.Show("Please enter valid Facility, Date, Name Of Customer, Time Of Booking and Hours"); 
      errorFacility.SetError(Facility, "Enter A Facility To Book"); 
     } 
     else if (Date.Text == "") 
     { 
      errorDate.SetError(Date, "Enter A Valid Date"); 
     } 
     else if (NameOfCustomer.Text == "") 
     { 
      errorCust.SetError(NameOfCustomer, "Enter A Customer To Book"); 
     } 
     else if (TimeOfBooking.Text == "") 
     { 
      errorTime.SetError(TimeOfBooking, "Enter A Time To Book"); 
     } 
     else if (Hours.Text == "") 
     { 
      errorHours.SetError(Hours, "Enter The Hours To Book"); 
     } 

     //so if there isnt no error in the fields itll go on and add the data in to the database. 
     else 
     { 

     //instance of sqlConnection    
     SqlConnection con = new SqlConnection("Data Source=..."); 

     //instance of sqlCommand 
     String query = 
      "INSERT INTO [Booking] values ('" 
      + Facility.Text 
      + "', '" + Date.Text 
      + "', '" + NameOfCustomer.Text 
      + "', '" + TimeOfBooking.Text 
      + "', '" + Hours.Text 
      + "', '" + Paid.Text 
      + "', '" + Notes.Text 
      + "')"; 

     SqlCommand cmd = new SqlCommand(query, con); 
     con.Open(); 
     cmd.ExecuteNonQuery(); 


     //query executed correcty or not 
     con.Close(); 

      MessageBox.Show("Sucessfully Booked"); 

      Form3 mainPage = new Form3(); 
      mainPage.Show(); 
      this.Close(); 
     } 
    } 
    }} 

이것은 내가 예약을 추가하는 데 사용하는 형식입니다.

 public partial class frmViewBookings : Form 
     { 
      public frmViewBookings() 
      { 
       InitializeComponent(); 
      } 

      private void btnClose_Click(object sender, EventArgs e) 
      { 
       Form3 mainpage = new Form3(); 
       mainpage.Show(); 
       this.Close(); 
      } 

      private void frmViewBookings_Load(object sender, EventArgs e) 
      { 
       // TODO: This line of code loads data into the 'usersDataSet1.Booking' table. You can move, or remove it, as needed. 
       this.bookingTableAdapter.Fill(this.usersDataSet1.Booking); 

      } 
     } 
    } 
+0

시도 할 것이다 (DataGridView를로드 할 수있는 별도의 방법을 만듭니다). frmViewBookings 양식을 만들고 표시하는 위치를 확인해야합니다. 이 코드를 제공 할 수 있습니까? – Rob

+0

나는 그것을 분할, 상단 내 frmBooking 양식 및 하단 frmViewBooking 양식입니다 ... 이것은 내가 지금까지 가지고있는 모든 것입니다 – bandaa

+0

나는 이해하지만 어딘가에 비슷한 코드 줄을 가지고 : frmViewBookings newFrmViewBookings = new frmViewBookings(); newFrmViewBookings.Show(); 네가 어디에서 그걸 보여줄 수 있니? – Rob

답변

0

하여 데이터 그리드에 데이터를 표시하려면, 당신은 그것의 설정해야합니다 : 내가이 두 번째 형태는 내가 즉 위해 다음과 같이 예약 코드를 만들 때 업데이트 할 DataGridView를 가진 하나입니다 당신은 당신은 다시 TableAdapter에를 작성하고 그것을 당신의 gridview의 데이터 소스를 설정해야합니다 새로 고침 할 때마다이

datagridview1.Datasource = this.usersDataSet1.Booking.DefaultView

같이 할 것이다 데이터 소스 첫째.

는 다시