0

我想将这个非常大的数据显示到 DataGridView 中。但我不知道该怎么做。

我在 Sql 表中的数据

http://s13.postimg.org/5dodas0k7/data_di_tabel.png

我希望数据像这样显示

http://s18.postimg.org/apnrbbxrd/tampilan_data_yg_diinginkan.png

我无法使用 SqlDataAdapter 显示数据,所以我在我的 cx 类中使用 SqlDataReader。这是我的代码:

public partial class frmPartProduction : DevExpress.XtraEditors.XtraForm
{
    public frmPartProduction()
    {
        InitializeComponent();
    }

    private void frmPartProduction_Load(object sender, EventArgs e)
    {
        txtTglAwal.EditValue = DateTime.Now;
        txtTglAkhir.EditValue = DateTime.Now.AddMonths(1);
        addRow();
    }

    private void addRow()
    {
        cx.Sql = "select PartNumber,PartName,BeginingStok from Part";
        cx.Execr();
        while (cx.Read())
        {
            String str = "";
            for (int i = 0; i < 8; i++)
            {
                if (i == 0) str = "Forecast";
                if (i == 1) str = "Stok Condition by Forecast";
                if (i == 2) str = "Production";
                if (i == 3) str = "Stok Condition by Production";
                if (i == 4) str = "Safety Stok";
                if (i == 5) str = "Schedjule Receipt";
                if (i == 6) str = "Outgoing";
                if (i == 7) str = "PO Release";

                DG.Rows.Add();
                int row = DG.RowCount - 1;
                if (i == 0)
                    DG.Rows[row].Cells[0].Style.ForeColor = Color.Black;
                else
                    DG.Rows[row].Cells[0].Style.ForeColor = Color.White;

                DG.Rows[row].Cells[0].Value = cx.getString("PartNumber");
                DG.Rows[row].Cells[1].Value = cx.getString("PartName");
                DG.Rows[row].Cells[2].Value = str;
                DG.Rows[row].Cells[3].Value = cx.getString("BeginingStok");
            }
        }
    }

    private void btnView_Click(object sender, EventArgs e)
    {
        while (DG.ColumnCount > 4)
        {
            DG.Columns.RemoveAt(4);
        }

        DateTime tgl_awal = Convert.ToDateTime(txtTglAwal.Text);
        DateTime tgl_akhir = Convert.ToDateTime(txtTglAkhir.Text);
        while (tgl_awal <= tgl_akhir)
        {
            if (tgl_awal.ToString("dddd") != "Saturday" && tgl_awal.ToString("dddd") != "Sunday")
            {
                DG.Columns.Add("", tgl_awal.ToString("dd-MMM-yyyy"));
                DG.Columns[DG.ColumnCount - 1].Width = 80;
            }
            tgl_awal = tgl_awal.AddDays(1);
        }

        DateTime tgl;
        for (int row = 0; row < DG.RowCount; row++)
        {
            String PartNumber = DG.Rows[row].Cells[0].Value.ToString();
            String Description = DG.Rows[row].Cells[2].Value.ToString();
            for (int col = 4; col < DG.ColumnCount; col++)
            {
                tgl = Convert.ToDateTime(DG.Columns[col].HeaderText);
                if (Description == "Safety Stok")
                {
                    cx.Sql = "select LimitStok from Part where PartNumber = @PartNumber";
                    cx.Param("@PartNumber", PartNumber);
                    cx.Param("@Description", Description);
                    cx.Param("@tgl", tgl);
                    cx.Execr();
                    if (cx.Read())
                    {
                        DG.Rows[row].Cells[col].Value = cx.getInt("LimitStok");
                    }
                    else
                    {
                        DG.Rows[row].Cells[col].Value = 0;
                    }
                }
                else
                {
                    cx.Sql = "select qty from PartProduction where PartNumber = @PartNumber and tgl = @tgl and Description = @Description";
                    cx.Param("@PartNumber", PartNumber);
                    cx.Param("@Description", Description);
                    cx.Param("@tgl", tgl);
                    cx.Execr();
                    if (cx.Read())
                    {
                        DG.Rows[row].Cells[col].Value = cx.getFloat("qty");
                    }
                    else
                    {
                        DG.Rows[row].Cells[col].Value = 0;
                    }
                }
            }
        }
    }
}

我使用 addRow() 方法初始化了 DataGrid 列,并在 btnView_Click 中添加了日期列。

我如何在水平显示中显示日期?:-(例如,

桌子

PartNumber PartName 描述 Tgl 数量

100 第 X 部分预测 01-10-2013 10

100 第 X 部分预测 02-10-2013 10

100 第 X 部分预测 03-10-2013 10

我想要数据网格显示是这样的

数据网格显示

PartNumber PartName 01-10-2013 02-10-2013 03-10-2013

100 第 X 部分 0 0 2

101 部分 Y 0 0 2

102 Z 部分 2 0 2

4

1 回答 1

0

You can use the Following code to bind the Data from database to GridView.

Please change the ConnectionString and SqlQuery asper your requirement.

private void getData()
    {
        String strCon = "Data Source=(local);Initial Catalog=database;uid=uid;pwd=pwd;Integrated Security=True;";
        using (SqlConnection sqlCon = new SqlConnection(strCon))
        {
            String strCmd = "select PartNumber,PartName,BeginingStok from Part";
            using (SqlCommand sqlcommand = new SqlCommand(strCmd, sqlCon))
            {
                sqlCon.Open();
                using (SqlDataAdapter sqlAdapter = new SqlDataAdapter(sqlcommand))
                {
                    DataSet ds = new DataSet();
                    sqlAdapter.Fill(ds);
                    //DG.DataSource = ds.Tables[0];

                    foreach (DataRow newrow in ds.Tables[0].Rows)
                    {


                        String str = "";
                        for (int i = 0; i < 8; i++)
                        {
                            if (i == 0) str = "Forecast";
                            if (i == 1) str = "Stok Condition by Forecast";
                            if (i == 2) str = "Production";
                            if (i == 3) str = "Stok Condition by Production";
                            if (i == 4) str = "Safety Stok";
                            if (i == 5) str = "Schedjule Receipt";
                            if (i == 6) str = "Outgoing";
                            if (i == 7) str = "PO Release";

                            DG.Rows.Add();
                            int row = DG.RowCount - 1;
                            if (i == 0)
                                DG.Rows[row].Cells[0].Style.ForeColor = Color.Black;
                            else
                                DG.Rows[row].Cells[0].Style.ForeColor = Color.White;

                            DG.Rows[row].Cells[0].Value = newrow["PartNumber"];
                            DG.Rows[row].Cells[1].Value = newrow["PartName"];
                            DG.Rows[row].Cells[2].Value = str;
                            DG.Rows[row].Cells[3].Value = newrow["BeginingStok"];
                        }


                    }
                }
            }
        }
    }
于 2013-11-15T07:41:10.123 回答