c#使用按钮向上或向下移动数据网格视图

时间:2015-03-12 03:27:20

标签: c# datagridview dataset

我已经创建了一个我目前面临的问题的示例程序。由于windows对象的性质,您不能简单地将对象或整数转换为数据网格视图作为只读,因此我无法复制数据集的当前位置并将该位置用于数据网格视图。我已设法获取数据网格视图,以显示在单击单元格时选择的视图,但无法使用按钮进行导航。下面是一个示例图片;

enter image description here

正如您所看到的,文本框中的值与数据网格视图不同,我的意图是使用按钮移动所选值,这将匹配文本框中的相同值。

    private void fill_table()
    {
        SqlConnection connection = new SqlConnection();
        try
        {
            connection.ConnectionString = connectionPath;
            connection.Open();

            SqlCommand cmd = new SqlCommand("SELECT * FROM Users", connection);
            SqlDataAdapter dap = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            dap.Fill(ds);

            bs.DataSource = ds.Tables[0];
            dgv.DataSource = bs;
            dap.Update(ds);              

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                object u = dr["Username"];
                string conversion_u = (string)u;
                string decrypted_u = Decrypt(conversion_u.Trim(), password);
                dr["Username"] = decrypted_u;

                object p = dr["Password"];
                string conversion_p = (string)p;
                string decrypted_p = Decrypt(conversion_p.Trim(), password);
                dr["Password"] = decrypted_p;

                object t = dr["Time"];
                string conversion_t = (string)t;
                string decrypted_t = Decrypt(conversion_t.Trim(), password);
                dr["Time"] = decrypted_t;
            }

        }
        catch (SqlException sql)
        {
            MessageBox.Show(sql.Message);
        }
        finally
        {
            connection.Close();
            connection.Dispose();
        }
    }

这是用于加载数据网格视图对象

中的值的当前代码
    private void btnFirst_Click(object sender, EventArgs e)
    {
        if (inc != MaxRows + 1)
        {
            inc = 0;
            MaxRows = 0;
            fill_textBoxes();
        }

    }

    private void btnPrevious_Click(object sender, EventArgs e)
    {
        try
        {
            if (inc != MaxRows + 1)
            {
                inc--;
                fill_textBoxes();
            }
        }
        catch (IndexOutOfRangeException)
        {
            DialogResult dlgResult;
            dlgResult = MessageBox.Show(
                    "First record",
                    "Records",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Warning,
                    MessageBoxDefaultButton.Button1);
        }
    }

    private void btnNext_Click(object sender, EventArgs e)
    {
        if (inc != MaxRows - 1)
        {
            inc++;
            fill_textBoxes();
        }
        else
            MessageBox.Show("No more records");
    }

    private void btnLast_Click(object sender, EventArgs e)
    {
        if (inc != MaxRows - 1)
        {
            inc = MaxRows - 1;
            fill_textBoxes();

        }
    }
    private void dgv_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        if (e.RowIndex >= 0)
        {
            DataGridViewRow row = dgv.Rows[e.RowIndex];

            txtdeuser.Text = row.Cells["Username"].Value.ToString();
            txtdepass.Text = row.Cells["Password"].Value.ToString();
            dtp.Text = row.Cells["Time"].Value.ToString();
        }
    }

这是当前导航文本框的代码,如果在数据网格视图中单击了值,则更改字段。

1 个答案:

答案 0 :(得分:0)

继承了我设法让它运作的代码

    pos = dgv.CurrentCell.RowIndex;

    private void btnFirst_Click(object sender, EventArgs e)
    {
        if (inc != MaxRows + 1)
        {
            inc = 0;
            MaxRows = 0;
            fill_textBoxes();

            if (pos < dgv.RowCount)
            {
                dgv.Rows[pos].Selected = false;
                dgv.Rows[pos = 0].Selected = true;
            }
        }           
    }
    private void btnPrevious_Click(object sender, EventArgs e)
    {
        try
        {
            if (inc != MaxRows + 1)
            {
                inc--;
                fill_textBoxes();

                if (pos < dgv.RowCount)
                {
                    dgv.Rows[pos].Selected = false;
                    dgv.Rows[--pos].Selected = true;
                }
            }
        }
        catch (IndexOutOfRangeException)
        {
            DialogResult dlgResult;
            dlgResult = MessageBox.Show(
                    "First record",
                    "Records",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Warning,
                    MessageBoxDefaultButton.Button1);
        }
    }

    private void btnNext_Click(object sender, EventArgs e)
    {
        if (inc != MaxRows - 1)
        {
            inc++;
            fill_textBoxes();

            if (pos < dgv.RowCount)
            {
                dgv.Rows[pos].Selected = false;
                dgv.Rows[++pos].Selected = true;
            }
        }
        else
            MessageBox.Show("No more records");
    }

    private void btnLast_Click(object sender, EventArgs e)
    {
        if (inc != MaxRows - 1)
        {
            inc = MaxRows - 1;
            fill_textBoxes();

            if (pos < dgv.RowCount)
            {
                dgv.Rows[pos].Selected = false;
                dgv.Rows[pos = MaxRows - 1].Selected = true;
            }
        }
    }