C#如何以简洁正确的编码方式缩短Datagridview Selectionchanged的代码

时间:2018-09-21 02:27:26

标签: c# winforms

我一直希望我的代码更简洁易读。我在这里是为了实现这一目标。由于我是初学者,所以最好早点学习。就像在类中调用所有这些代码一样,我也不想在表单中看到这么多代码。我希望有人能够给我一些建议和正确的做法。

这是我的代码

 public partial class SIMSSupplier : UserControl
{
    ADDSupplier supply;
    ADDPReturns returns;
    public SIMSSupplier()
    {
        InitializeComponent();

    }
    public DataTable dbdataset;
    public DataSet ds = new DataSet();
    public string ID = "SPPLR-000";
    public int DeliveryID, OrderID, ReturnID;
    DataView db;
    DataTable dt = new DataTable();
    private void Supplierview_SelectionChanged(object sender, EventArgs e)
    {
        var row = Supplierview.CurrentCell.RowIndex;
        SupplierID.Text = Supplierview.Rows[row].Cells[0].Value.ToString();
        CompanyName.Text = Supplierview.Rows[row].Cells[1].Value.ToString();
        ContactName.Text = Supplierview.Rows[row].Cells[2].Value.ToString();
        ContactNumber.Text = Supplierview.Rows[row].Cells[3].Value.ToString();
        Date.Text = Supplierview.Rows[row].Cells[4].Value.ToString();
        Address.Text = Supplierview.Rows[row].Cells[5].Value.ToString();
        Remarks.Text = Supplierview.Rows[row].Cells[6].Value.ToString();

    }
    private void PurchaseOrder_SelectionChanged(object sender, EventArgs e)
    {
        var row = PurchaseOrder.CurrentCell.RowIndex;
        txt_purchase.Text = PurchaseDeliveries.Rows[row].Cells[0].Value.ToString();
        txt_supplier.Text = PurchaseDeliveries.Rows[row].Cells[1].Value.ToString();
        txt_item.Text = PurchaseDeliveries.Rows[row].Cells[2].Value.ToString();
        txt_date.Text = PurchaseDeliveries.Rows[row].Cells[3].Value.ToString();
        txt_quantity.Text = PurchaseDeliveries.Rows[row].Cells[4].Value.ToString();
        txt_cost.Text = PurchaseDeliveries.Rows[row].Cells[5].Value.ToString();
        txt_amount.Text = PurchaseDeliveries.Rows[row].Cells[6].Value.ToString();
        txt_sales.Text = PurchaseDeliveries.Rows[row].Cells[7].Value.ToString();
        txt_code.Text = PurchaseDeliveries.Rows[row].Cells[8].Value.ToString();
        txt_patient.Text = PurchaseDeliveries.Rows[row].Cells[9].Value.ToString();
    }
    private void PurchaseDeliveries_SelectionChanged(object sender, EventArgs e)
    {
        var row = PurchaseDeliveries.CurrentCell.RowIndex;
        PurchaseID.Text = PurchaseDeliveries.Rows[row].Cells[0].Value.ToString();
        Supplier.Text = PurchaseDeliveries.Rows[row].Cells[1].Value.ToString();
        ItemDescription.Text = PurchaseDeliveries.Rows[row].Cells[2].Value.ToString();
        Dates.Text = PurchaseDeliveries.Rows[row].Cells[3].Value.ToString();
        Quantity.Text = PurchaseDeliveries.Rows[row].Cells[4].Value.ToString();
        Unitcost.Text = PurchaseDeliveries.Rows[row].Cells[5].Value.ToString();
        Amount.Text = PurchaseDeliveries.Rows[row].Cells[6].Value.ToString();
        SalesInvoice.Text = PurchaseDeliveries.Rows[row].Cells[7].Value.ToString();
        Codeitems.Text = PurchaseDeliveries.Rows[row].Cells[8].Value.ToString();
        Patientname.Text = PurchaseDeliveries.Rows[row].Cells[9].Value.ToString();

    }
    private void PurchaseReturn_SelectionChanged(object sender, EventArgs e)
    {
        var row = PurchaseReturn.CurrentCell.RowIndex;
        txt_return.Text = PurchaseReturn.Rows[row].Cells[0].Value.ToString();
        txt_rsupplier.Text = PurchaseReturn.Rows[row].Cells[1].Value.ToString();
        txt_ritem.Text = PurchaseReturn.Rows[row].Cells[2].Value.ToString();
        txt_rmodel.Text = PurchaseReturn.Rows[row].Cells[3].Value.ToString();
        txt_rsrp.Text = PurchaseReturn.Rows[row].Cells[4].Value.ToString();
        txt_rcode.Text = PurchaseReturn.Rows[row].Cells[5].Value.ToString();
        txt_rdate.Text = PurchaseReturn.Rows[row].Cells[6].Value.ToString();
        txt_rremarks.Text = PurchaseReturn.Rows[row].Cells[7].Value.ToString();
    }
}

2 个答案:

答案 0 :(得分:1)

第一个可以简化如下:

private void Supplierview_SelectionChanged(object sender, EventArgs e)
{
    var row = Supplierview.CurrentRow;
    SupplierID.Text = row.Cells[0].Value.ToString();
    CompanyName.Text = row.Cells[1].Value.ToString();
    ContactName.Text = row.Cells[2].Value.ToString();
    ContactNumber.Text = row.Cells[3].Value.ToString();
    Date.Text = row.Cells[4].Value.ToString();
    Address.Text = row.Cells[5].Value.ToString();
    Remarks.Text = row.Cells[6].Value.ToString();
}

第二个,我建议使用对象集合作为网格的数据源。例如:

class DataItem{
    public string SupplierID {get;set;}
    public string CompanyName {get;set;}
    .....
}

Supplierview.DataSource = "collection of DataItem"

然后

private void Supplierview_SelectionChanged(object sender, EventArgs e)
{
    var dataItem = dataGridView1.CurrentRow.DataBoundItem as DataItem;
    if (dataItem != null)
    {
        SupplierID.Text = dataItem.SupplierID;
        .....
    }
}

答案 1 :(得分:0)

我建议为此使用DataBinding,这样就不需要代码来执行示例中的操作。
如果您不想使用数据绑定,则可以简单地使用私有方法来组织代码。
例如

#region Supplier Stuff

private void SupplierViewChanged(DataRow row)
{
    SupplierID.Text = row.Cells[0].Value.ToString();
    CompanyName.Text = row.Cells[1].Value.ToString();
    ContactName.Text = row.Cells[2].Value.ToString();
    ContactNumber.Text = row.Cells[3].Value.ToString();
    Date.Text = row.Cells[4].Value.ToString();
    Address.Text = row.Cells[5].Value.ToString();
    Remarks.Text = row.Cells[6].Value.ToString();    
}

// put all other helper methods that deal with Supplier here...

#endregion Supplier Stuff


private void Supplierview_SelectionChanged(object sender, EventArgs e)
{
    SupplierViewChanged(Supplierview.CurrentRow);
}

这使您的代码更具组织性和可读性,但是数据绑定仍然是我会选择的方法