0

在下面找到我用来将gridview中选定行的值传递到另一个表单上的文本框的代码示例;有这样的场景:我有 2 个表单第一个表单包含一个按钮和文本框,该按钮打开第二个表单“表单 2”。form2 包含 Xtragrid ,我想从这个表单执行双击事件以加载第一个表单中的每个选定行值..

好吧,我已经用你的方法修改了我的代码,但什么都没发生,也许我忽略了一些东西,如果你也可以看看,也许你可能会从我这边想出一些东西我仍然不明白为什么????

// Class SEarchLessee
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace YoungWoman
{
public class SearchLessee 
{
    public int Lesseeid { get; set; }
    public string Name { get; set; }
    public string LastName { get; set; }
}




}

 // custom event

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;

 namespace YoungWoman
 {
  public class CustomDoubleClickEventArgs : EventArgs
  {
    public SearchLessee SearchLessee { get; set; }
  }

  }

   // form 2 with Xtragrid

   using System;
   using System.Collections.Generic;
   using System.ComponentModel;
   using System.Data;
   using System.Drawing;
   using System.Linq;
   using System.Text;
   using System.Windows.Forms;
   using System.Data.SqlClient;
   using DevExpress.XtraGrid.Views.Grid;
   using DevExpress.XtraEditors;


   namespace YoungWoman
   {
       public partial class SelectLessee : XtraForm
         {

    SqlConnection conne = SqlCoonectionSEtup.GetConnection;
    DataTable dt = new DataTable();

    public event EventHandler<CustomDoubleClickEventArgs> CustomViewDoubleClick;

    public SelectLessee()
    {
        InitializeComponent();
    }
   // private List<IUserObserver> ObserverObject = new List<IUserObserver>();



    private void AddLesseebtn_Click(object sender, EventArgs e)
    {
        Lesseefrm frm = new Lesseefrm(Utils.Formtype.add, 0);
        frm.SelectEventHandler += new EventHandler(RefreshGrid);
        frm.ShowDialog();


    }

    private void editbtn_Click(object sender, EventArgs e)
    {
        Lesseefrm frm1 = new Lesseefrm(Utils.Formtype.edit, 1);
        frm1.ShowDialog();
    }



    private void SelectLessee_Load(object sender, EventArgs e)
    {



        SqlDataAdapter sda = new SqlDataAdapter("SELECT  *FROM LesseeYW", conne);
        (dt).Clear();
        sda.Fill(dt);
        LesseegridControl.DataSource = dt;
        RefreshGrid();
        GridView.Columns[1].Visible = false;
        GridView.Columns[4].Visible = false;
        GridView.Columns[5].Visible = false;
        GridView.Columns[6].Visible = false;
        GridView.Columns[7].Visible = false;
        GridView.Columns[9].Visible = false;
        GridView.Columns[10].Visible = false;
        GridView.Columns[11].Visible = false;
        GridView.Columns[12].Visible = false;
        GridView.Columns[13].Visible = false;
        GridView.Columns[15].Visible = false;
        GridView.Columns[16].Visible = false;
        GridView.Columns[17].Visible = false;
        GridView.Columns[19].Visible = false;





    }
    void RefreshGrid(object sender, EventArgs e)
    {


        RefreshGrid();

    }

    private void RefreshGrid()
    {
        SqlDataAdapter sda = new SqlDataAdapter("SELECT  *FROM LesseeYW", conne);
        (dt).Clear();
        sda.Fill(dt);
        LesseegridControl.DataSource = dt;
        LesseegridControl.RefreshDataSource();
    }

    private void Searchbtn_Click(object sender, EventArgs e)
    {
        if (radioButton1.Checked)
        {
            txtPhoneNo.Enabled = false;
            txtPassportNo.Enabled = false;
            txtIdNo.Enabled = false;



            SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM LesseeYW WHERE LesseeId =" + int.Parse(txtLesseeId.Text), conne);
            (dt).Clear();
            sda.Fill(dt);
            LesseegridControl.DataSource = dt;

            GridView.Columns[1].Visible = false;
            GridView.Columns[4].Visible = false;
            GridView.Columns[5].Visible = false;
            GridView.Columns[6].Visible = false;
            GridView.Columns[7].Visible = false;
            GridView.Columns[9].Visible = false;
            GridView.Columns[10].Visible = false;
            GridView.Columns[11].Visible = false;
            GridView.Columns[12].Visible = false;
            GridView.Columns[13].Visible = false;
            GridView.Columns[15].Visible = false;
            GridView.Columns[16].Visible = false;
            GridView.Columns[17].Visible = false;
            GridView.Columns[19].Visible = false;

        }


        if (radioButton2.Checked)
        {
            txtLesseeId.Enabled = false;
            txtPassportNo.Enabled = false;
            txtPhoneNo.Enabled = false;

        }



        if (radioButton3.Checked)
        {
            txtPassportNo.Enabled = false;
            txtLesseeId.Enabled = false;
            txtIdNo.Enabled = false;
        }


        if (radioButton4.Checked)
        {
            txtIdNo.Enabled = false;
            txtLesseeId.Enabled = false;
            txtPhoneNo.Enabled = false;
        }

    }



    private void DisplayallBtn_Click(object sender, EventArgs e)
    {


        SqlDataAdapter sda = new SqlDataAdapter("SELECT  *FROM LesseeYW", conne);
        (dt).Clear();
        sda.Fill(dt);
        LesseegridControl.DataSource = dt;
        GridView.Columns[1].Visible = false;
        GridView.Columns[4].Visible = false;
        GridView.Columns[5].Visible = false;
        GridView.Columns[6].Visible = false;
        GridView.Columns[7].Visible = false;
        GridView.Columns[9].Visible = false;
        GridView.Columns[10].Visible = false;
        GridView.Columns[11].Visible = false;
        GridView.Columns[12].Visible = false;
        GridView.Columns[13].Visible = false;
        GridView.Columns[15].Visible = false;
        GridView.Columns[16].Visible = false;
        GridView.Columns[17].Visible = false;
        GridView.Columns[19].Visible = false;

    }

    private void GridView_DoubleClick(object sender, EventArgs e)
    {
        if (CustomViewDoubleClick == null)
            return; 

         CustomViewDoubleClick(this, new CustomDoubleClickEventArgs() 

         {
             SearchLessee = new SearchLessee()

             {
               LastName = GridView.GetFocusedRowCellValue("LesseeLastName").ToString(),
               Name =GridView.GetFocusedRowCellValue("LesseeName").ToString(),
               Lesseeid =Convert.ToInt32(GridView.GetFocusedRowCellValue("LesseeId"))
             }

         });
       }



     }
   }

    // form1 showing the data

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using YoungWoman.Utils;
    using YoungWoman.Properties;
    using DevExpress.XtraGrid.Views.Grid;
    using DevExpress.XtraEditors;


    namespace YoungWoman
     {
         public partial class Reservationfrm : XtraForm
        {
           SelectLessee SearchLs;

               public Reservationfrm()
           {
                InitializeComponent();

           }

    private Formtype formtype = Formtype.none;
    private int Reservationid = 0;



    public Reservationfrm(Formtype formtype , int Reservationid)
    {
        InitializeComponent();
        this.formtype = formtype;
        this.Reservationid = Reservationid;
    }





    private void Reservationfrm_Load(object sender, EventArgs e)
    {

        SearchLs = new SelectLessee();
        SearchLs.CustomViewDoubleClick += OnDoubleClick;

        dateEdit1.DateTime = DateTime.Today;
        dateEdit2.DateTime = DateTime.Today.AddDays(1);
        ChangingForm();

    }

    private void ChangingForm()
    {
        if (formtype == Formtype.add)
        {
            ReservationIdtextEdit.Enabled = false;
            this.Text = string.Format(" New Reservation");

        }
        else
            if (formtype == Formtype.edit && Reservationid > 0)
            {
                ReservationIdtextEdit.Enabled = false;
                SaveBtn.Text = "&Edit";
                SaveBtn.Image = Resources.brush_16;
                this.Text = string.Format(" Edit Reservation ");

            }
            else
                if (formtype == Formtype.addSelected && Reservationid > 0)
                {
                    ReservationIdtextEdit.Enabled = false;
                    this.Text = string.Format(" Add Reservation Of the Selected Lessee ");
                }
                else
                    if (formtype == Formtype.EditSelected && Reservationid > 0)
                    {
                        ReservationIdtextEdit.Enabled = false;
                        SaveBtn.Text = "&Edit";
                        SaveBtn.Image = Resources.brush_16;
                        this.Text = string.Format("Edit Reservation Of the Selected Lessee");
                    }
                    else
                        if (formtype == Formtype.addselected2 && Reservationid > 0)
                        {
                            ReservationIdtextEdit.Enabled = false;
                            this.Text = string.Format(" Add Reservation Of the Selected Room ");
                        }
                        else
                            if (formtype == Formtype.Editselected2 && Reservationid > 0)
                            {
                                ReservationIdtextEdit.Enabled = false;
                                SaveBtn.Text = "&Edit";
                                SaveBtn.Image = Resources.brush_16;
                                this.Text = string.Format(" Edit Reservation of The Selected Room");
                            }

    }

    private void OnDoubleClick(object sender, CustomDoubleClickEventArgs args)
    {
        txtLesseeName.Text = args.SearchLessee.Name;
        LastNamelabelControl.Text = args.SearchLessee.LastName;
        LesseeIdlabelControl.Text = args.SearchLessee.Lesseeid.ToString();

    }

    private void SelectLesseeBtn_Click(object sender, EventArgs e)
    {
        SearchLs = new SelectLessee();
        SearchLs.Show();

    }

    private void SelectRoom_Click(object sender, EventArgs e)
    {
        SelectRoomfrm slr = new SelectRoomfrm();

        slr.ShowDialog();
    }

    private void SelectEmployee_Click(object sender, EventArgs e)
    {
        SelectEmployeefrm empfrm = new SelectEmployeefrm();
        empfrm.ShowDialog();
    }

    void ClearField()
    {
        dateEdit1.EditValue = null;

        dateEdit2.EditValue = null;
        dateEdit3.EditValue = null;
        txtLesseeName.Text = null;
        LesseeIdlabelControl.Text = null;
        LastNamelabelControl.Text = null;
        textEdit3.Text = null;
        textEdit4.Text = null;
    }

    private void Dates_values(object sender, EventArgs e)
    {

    }

    private void Dates_values2(object sender, EventArgs e)
    {

        DateTime dtarrival = dateEdit1.DateTime;
        DateTime dtdeparture = dateEdit2.DateTime;

        if (dtarrival == dtdeparture)
        {
            MessageBox.Show(" Arrival Date cannot be the same as departure Date , Please Change Dates ", " Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            SaveBtn.Enabled = false;

        }
        else
        {
            SaveBtn.Enabled = true;
        }

      }





     }
    }
4

1 回答 1

0

我的方法是使用事件,在您的表单中创建一个包含您的数据的事件,以及一个包含您希望显示的值的自定义事件参数。下面的代码演示了我是如何做到的。希望这会有所帮助

你可以在这里找到解决方案

//Issue
public class Issue
{
    public string Value1 { get; set; }
    public int Value2 { get; set; }
}

//My custom evenntArgs
public class CustomDoubleClickEventArgs : EventArgs
{
    public Issue Issue { get; set; }
}

//Form 2 : Containing the xtragrid

public event EventHandler<CustomDoubleClickEventArgs> CustomViewDoubleClick;

//Handle the view double click event
private void GridView_DoubleClick(object sender, EventArgs e)
{
    if (CustomViewDoubleClick == null)
        return;

    CustomViewDoubleClick(this, new CustomDoubleClickEventArgs() 
    { 
        Issue = new Issue() 
        {  
            //You can use GetFocusedRowCellValue(col) rather than GetRowCellValue(handle, col)
            Value1 = GridView.GetFocusedRowCellValue("Name").ToString(),
            Value2 = Convert.ToInt32(GridView.GetFocusedRowCellValue("Age"))
        }
    });
}

//Form 1 : Showing the focused row data
Form2 form2;
private void Form1_Load(object sender, EventArgs e)
{
    form2 = new Form2();
    form2.CustomViewDoubleClick += OnDoubleClick;
}

private void OnDoubleClick(object sender, CustomDoubleClickEventArgs args)
{
    txFirstName.Text = args.Issue.Value1;
    txAge.Text = args.Issue.Value2.ToString();
}

private void bnShowData_Click(object sender, EventArgs e)
{
    form2.Show();
}
于 2012-07-29T07:59:23.937 回答