如何使算术列保持静止 [英] How to make arithmetric column stay static

查看:81
本文介绍了如何使算术列保持静止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有两个按钮,当点击(sp)时从sql服务器获取它的记录。

两个按钮都正确地获取datagridview。

dgv列为ff(A,B,C和D)。 C列是(A - B)。

如果我点击btn1它完全显示我的记录但是如果我点击btn2然后点击btn1,它会将记录显示为ff(A,B, D和C)。但那不是我所期待的。

是否有解决方案使记录显示为sp? (A,B,C,D)





Hi I've two buttons which gets its records fron sql server when clicked (sp).
Both Buttons are fetching the datagridview corerectly.
The dgv columns are as ff (A,B,C and D). Column C is (A - B).
If i click btn1 it displays me the records perfectly BUT if i click btn2 and the later click btn1, it displays me the records as ff (A,B,D and C). But that's not what i'm expecting.
Is there a solution to make the records display as in the sp? (A,B,C,D)


private void btn1_Click(object sender, EventArgs e)
            {
                if (cbBebene.SelectedItem != null)
                {
                    string C = ConfigurationManager.ConnectionStrings["S"].ConnectionString;
                    SqlConnection con = new SqlConnection(C);

        // TSQL Statement to choose tables dynamic
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = ("[dbo].[spBeFKZ]");
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@Table_Name", cbBebene.SelectedValue.ToString());
        SqlDataAdapter adapter = new SqlDataAdapter(cmd);                

        try
        {
            con.Open();
            dt = new System.Data.DataTable();
            adapter.Fill(dt);
            dgvBerichte.AutoGenerateColumns = true;
            dgvBerichte.DataSource = dt;
                                                                              
        lbCount.Text = "Anzahl : " + dgvBerichte.Rows.Count.ToString();

                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                        finally
                        {
                            con.Close();
                        }                
                }
                else
                {
                    MessageBox.Show("OnClick safety control!!! Periode is empty");
                }

            }







private void btn2_Click(object sender, EventArgs e)
            {
                if (cbBebene.SelectedItem != null)
                {
                    //Connection string
                    string C = ConfigurationManager.ConnectionStrings["S"].ConnectionString;
                    SqlConnection con = new SqlConnection(C);

        // TSQL Statement to choose tables dynamic
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = ("[dbo].[spBeTp]");
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@Table_Name", cbBebene.SelectedValue.ToString());
        SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            try
            {
                con.Open();
                dt = new System.Data.DataTable();
                adapter.Fill(dt);
                dgvBerichte.AutoGenerateColumns = true;
                dgvBerichte.DataSource = dt;
                /* change the color of GWU Delta */
                dgvBerichte.Columns[3].DefaultCellStyle.ForeColor = Color.Blue;
                             
                lbCount.Text = "Anzahl : " + dgvBerichte.Rows.Count.ToString();

                        }

                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                        finally
                        {
                            con.Close();
                        }                   
                }
                else
                {
                    MessageBox.Show("OnClick safety control!!! Periode is empty");
                }

            }

推荐答案

三种解决方案:

1 )在存储过程中更改字段的顺序以适合ABCD。

2)不允许AutoGenerateColumns,定义您自己的订单并按列名绑定 - BEST ONE,您可以完全控制.. 。

3)取决于您拨打的SP,设置columnindex属性(最差,仅包括完整性)



如果这有帮助,请采取时间接受解决方案。谢谢。
Three solutions:
1) in the stored procedure change the order of the fields to fit ABCD.
2) don't allow AutoGenerateColumns, define your own order and bind by column name - BEST ONE, you have complete control...
3) depending on the SP you call, set columnindex property (worst and only included for completeness)

If this helps please take times to accept the solution. Thank you.


忽略列名,这些都是我的母语,显然你必须把它们自己。



Ignore the column names, these are in my native language, you have to put your own, obviously.

With dgv
AutoGenerateColumns = False
                .Columns(0).DataPropertyName = "redni_br"
                .Columns(1).DataPropertyName = "id_zad"
                .Columns(2).DataPropertyName = "id"
                .Columns(3).DataPropertyName = "id_kutije"
                .Columns(4).DataPropertyName = "status"
                .Columns(5).DataPropertyName = "greska"
                .Columns(6).DataPropertyName = "razlog"
                .Columns(7).DataPropertyName = "tip"

                .DataSource = source.Tables(1)
            End With





在网格设计器中,您可以找到列属性并设置列,标题,对齐,空值,格式等...然后你只需将它们在表单加载绑定到特定列...如果你fi你可以使用列名而不是索引并且它更具可读性。



您可以在表单加载时绑定列,只在可用时分配数据源,或者在分配源之前绑定它们。它不能以其他方式工作:)



如果这有帮助请花时间接受解决方案。谢谢。



In the grid designer you can find Columns property and set the columns, headers, alignement, null values, formats etc...then you just bind them in form load to particular column...you can use column names instead of indexes if you find it more readable.

You can bind the columns in form load and only assign datasource when it is available or you can bind them just before assigning the source. IT doesn't work other way around :)

If this helps please take time to accept the solution. Thank you.


这篇关于如何使算术列保持静止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆