在C#中自动验证 [英] Automatically validate in C#

查看:94
本文介绍了在C#中自动验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我试图将一个来自C#的事件用于TextBox,它可以在从DataGridView中选择一些内容之后,它将在TextBox中插入那些列值,然后详细说明TextBox中的内容。



我不想使用TextChanged



价值显示后我该怎么办? TextBox会去DataGridView吗?



我尝试过:



So, I am trying to use an Event from C# into the TextBox which could be able to after selecting something from a DataGridView it will insert that columns values in the TextBox and then detailed the info from what it is in the TextBox.

I don't want to use TextChanged

What could I do to after the value shows on TextBox it will go to DataGridView?

What I have tried:

public bool ValidateText()
        {
            bool Isvalidated = false;

            try
            {
                SqlConnection con = new SqlConnection(cs.DBConnP);
                con.Open();

                adp = new SqlDataAdapter();
                adp.SelectCommand = new SqlCommand(@"", con);
                
                ds = new DataSet("ds");
                adp.Fill(ds);
                dtable = ds.Tables[0];

                adp.Fill(ds, "CargaCab");

                dataGridView1.DataSource = ds.Tables["CargaCab"].DefaultView;

                foreach (DataRow row in dt.Rows)
                {
                    int n = dataGridView1.Rows.Add();
                    dataGridView1.Rows[n].Cells[0].Value = row[0].ToString();
                    dataGridView1.Rows[n].Cells[1].Value = row[1].ToString();
                    dataGridView1.Rows[n].Cells[2].Value = row[2].ToString();
                    dataGridView1.Rows[n].Cells[3].Value = row[3].ToString();
                }

                dataGridView1.Columns[0].ReadOnly = true;
                dataGridView1.Columns[1].ReadOnly = true;
                dataGridView1.Columns[2].ReadOnly = true;
                dataGridView1.Columns[3].ReadOnly = false;

                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Erro\nDetalhes: " + ex.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Isvalidated = false;
            }
            return Isvalidated;
        }







private void txtCarga_Validating(object sender, CancelEventArgs e)
        {
            ValidateText();

            int LinhasAfetadas = 0;
            LinhasAfetadas = dataGridView1.RowCount;
            label1.Text = "Número de registos: " + LinhasAfetadas;
        }

推荐答案

假设您的意思是使用数据网格视图中选择的内容填充文本框,请使用CellClick或CellEnter数据网格视图上的事件。 CellEnter更有用,因为它会在单元格点击时触发,并且当单元格被聚焦时(即使用箭头键在网格中导航)。



Assuming you mean populating the text box with what is selected in data grid view, use the CellClick or CellEnter events on the data grid view. CellEnter is more useful as it will trigger on cell click and when a cell is focused (i.e. using arrow keys to navigate in the grid).

private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e){
   object data = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;   
   txtCarga.Text = data.ToString();
}


这篇关于在C#中自动验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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