使用组合框值将数据从数据库重新加载到Datagridview进行编辑

时间:2019-01-09 06:38:20

标签: c# datagridview

我正在尝试使用组合框值将数据从数据库重新加载到datagridview进行编辑,但是代码在datagridview中又生成了一个PRODUCT列。屏幕截图和我的完整代码如下。我如何在PRODUCT的退出列中重新加载数据。 数据库名称:MyDb 表名称:tblSALEITEMDETAILS 列名:1)ID,2)PRODUCTNAME,3)QUANTITY,4)RATE,5)INVOICENO

enter image description here

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;
using System.Data.OleDb;

namespace invoiceedit
{
public partial class SALEINVOICE : Form
{
    static string conString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data 
Source=C:\Users\Ashfaque Baig\Documents\MyAccessDb\MyDb.accdb;Persist 
Security Info=False;";
    OleDbConnection con = new OleDbConnection(conString);
    OleDbCommand cmd = new OleDbCommand();
    DataTable dtProduct = new DataTable();
    DataTable dtCustomer = new DataTable();
    DataTable dtInvoiceNo = new DataTable();
    DataGridViewComboBoxColumn PRODUCTNAME = new 
    DataGridViewComboBoxColumn();


    public SALEINVOICE()
    {
        InitializeComponent();
    }

    private void SALEINVOICE_Load(object sender, EventArgs e)
    {
        CreatDataGridColumns();

        CUSTOMERLIST();
        combCUSTOMERNAME.DataSource = dtCustomer;
        combCUSTOMERNAME.DisplayMember = "CUSTOMERNAME";

        INVOICENOLIST();
        combINVOICENO.DataSource = dtInvoiceNo;
        combINVOICENO.DisplayMember = "INVOICENO";
    }
    private DataSet CUSTOMERLIST()
    {
        OleDbDataAdapter adapter = new OleDbDataAdapter();
        OleDbCommand cmd;
        DataSet dsCustomer = new DataSet();
        //SELECT STMTS

        string tblCustomer = "SELECT * FROM tblCUSTOMER";

        //PRODUCTTABLE
        cmd = new OleDbCommand(tblCustomer, con);
        adapter.SelectCommand = cmd;

        adapter.SelectCommand.CommandText = tblCustomer;
        adapter.Fill(dsCustomer, "CUSTOMERNAME");
        dtCustomer = dsCustomer.Tables[0];
        return dsCustomer;
    }

    private DataSet PRODUCTLIST()
    {
        OleDbDataAdapter adapter = new OleDbDataAdapter();
        OleDbCommand cmd;
        DataSet dsProduct = new DataSet();
        //SELECT STMTS

        string tblPRODUCT = "SELECT * FROM tblPRODUCT";

        //PRODUCTTABLE
        cmd = new OleDbCommand(tblPRODUCT, con);
        adapter.SelectCommand = cmd;

        adapter.SelectCommand.CommandText = tblPRODUCT;
        adapter.Fill(dsProduct, "PRODUCTNAME");
        dtInvoiceNo = dsProduct.Tables[0];
        return dsProduct;

    }

    private DataSet INVOICENOLIST()
    {
        OleDbDataAdapter adapter = new OleDbDataAdapter();
        OleDbCommand cmd;
        DataSet dsPurchaseGrnList = new DataSet();
        //SELECT STMTS

        string tblSALEBILDETAILS = "SELECT * FROM tblSALEBILDETAILS";

        //PRODUCTTABLE
        cmd = new OleDbCommand(tblSALEBILDETAILS, con);
        adapter.SelectCommand = cmd;

        adapter.SelectCommand.CommandText = tblSALEBILDETAILS;
        adapter.Fill(dsPurchaseGrnList, "INVOICENO");
        dtInvoiceNo = dsPurchaseGrnList.Tables[0];
        return dsPurchaseGrnList;
    }
    public void CreatDataGridColumns()
    {
        //ADD COLUMNS

        DataGridViewComboBoxColumn PRODUCTNAME = new 
DataGridViewComboBoxColumn();
        PRODUCTNAME.HeaderText = "PRODUCTNAME";
        PRODUCTNAME.Name = "PRODUCTNAME";
        PRODUCTNAME.AutoComplete = true;
        PRODUCTNAME.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
        PRODUCTNAME.DisplayMember = "PRODUCTNAME";
        PRODUCTNAME.Width = 200;

        ArrayList row = new ArrayList();
        //FILL ARRAYLIST FROM DATATABLE
        foreach (DataRow dr in dtProduct.Rows)
        {
            row.Add(dr["PRODUCTNAME"].ToString());
        }
        //ADD TO COMBO
        PRODUCTNAME.Items.AddRange(row.ToArray());

        //ADD THE COMBO TO DATAGRIDVIEW

        dataGridITEMDETAILS.Columns.Add(PRODUCTNAME);
        PRODUCTNAME.DataSource = PRODUCTLIST().Tables[0];



        DataGridViewColumnCollection columns = dataGridITEMDETAILS.Columns;


        DataGridViewTextBoxColumn textColumn = new 
DataGridViewTextBoxColumn();
        textColumn.DataPropertyName = "QUANTITY";
        textColumn.Name = "QUANTITY";
        textColumn.HeaderText = "QUANTITY";
        textColumn.ValueType = typeof(string);
        textColumn.Visible = true;
        columns.Add(textColumn);

        textColumn = new DataGridViewTextBoxColumn();
        textColumn.DataPropertyName = "RATE";
        textColumn.Name = "RATE";
        textColumn.HeaderText = "RATE";
        textColumn.ValueType = typeof(string);
        textColumn.Visible = true;
        columns.Add(textColumn);

        textColumn = new DataGridViewTextBoxColumn();
        textColumn.DataPropertyName = "AMOUNT";
        textColumn.Name = "AMOUNT";
        textColumn.HeaderText = "AMOUNT";
        textColumn.ValueType = typeof(string);
        textColumn.Visible = true;
        columns.Add(textColumn);



    }

    private void combINVOICENO_SelectedIndexChanged(object sender, EventArgs e)
    {
        cmd = new OleDbCommand("SELECT * FROM tblSALEBILDETAILS where INVOICENO='" + combINVOICENO.Text + "'", con);
        con.Open();
        cmd.ExecuteNonQuery();
        OleDbDataReader dr;
        dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            string InvoiceDate = (string)dr["INVOICEDATE"].ToString();
            string InvoiceNo = (string)dr["INVOICENO"].ToString();
            string CustomerName = (string)dr["CUSTOMERNAME"].ToString();


            txtDATE.Text = InvoiceDate;
            txtINVOICENO.Text = InvoiceNo;
            combCUSTOMERNAME.Text = CustomerName;

        }
        con.Close();

        {
            con.Open();
            OleDbCommand cmd = new OleDbCommand();
            cmd.Connection = con;

            string query = "SELECT PRODUCTNAME,QUANTITY,RATE FROM tblSALEITEMDETAILS where INVOICENO='" + combINVOICENO.Text + "'";

            cmd.CommandText = query;

            OleDbDataAdapter da = new OleDbDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            dataGridITEMDETAILS.DataSource = dt;
            PRODUCTNAME.DisplayMember = "PRODUCTNAME";

            con.Close();
        }
    }
}

}

0 个答案:

没有答案