如何在C#windows应用程序中单击列标题时对datagridview数据进行排序 [英] How to sort the datagridview data.when I click column header in C# windows application
问题描述
我在那里编辑和删除按钮有一个datagridview。我正在从数据库表中加载
datagridview。然后我点击datagridview编辑按钮各自的文本框中显示的相应数据。我和我点击提交按钮数据正在保存数据库并显示datagridview。为此目的使用了datagridview cellcontent click event。我想要的是如果我点击datagridview数据上的id列标题需要排序。怎么做。我的Argument超出了范围异常索引超出范围异常.Plaese帮我这个。
我尝试过:
I have a datagridview in that edit and delete buttons there.I am loading
datagridview from the database table.And I click the datagridview Edit button respective data showing in the respective textboxes fine.and i click submit button data is saving database and showing datagridview.For this purpose used datagridview cellcontent click event.what i want is if i click id column header on datagridview data needs to sorted.How to do that.I am getting Argument out of Range Exception "Index out of range Exception".Plaese help me on this.
What I have tried:
<blockquote class="quote"><div class="op">Quote:</div>
private void dgvwDtls_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
ChqMngrBsns.Masters.BankInfo bnkinfo = new ChqMngrBsns.Masters.BankInfo();
bnkid = Convert.ToInt32(dgvwDtls.Rows[e.RowIndex].Cells["bankid"].Value);
if (dgvwDtls.Columns[e.ColumnIndex].Name == "Edit" && e.RowIndex >= 0)
{
txtBnknm.Text = dgvwDtls.Rows[e.RowIndex].Cells["bankname"].Value.ToString();
string CTS = dgvwDtls.Rows[e.RowIndex].Cells["Chequetype"].Value.ToString();
if (CTS == "True")
{
rbchqtype1.Checked = true;
}
else if (CTS == "False")
{
rbchqtype2.Checked = true;
}
txtAcntno.Text = dgvwDtls.Rows[e.RowIndex].Cells["Accountno"].Value.ToString();
cmbAcntype.Text = dgvwDtls.Rows[e.RowIndex].Cells["Accounttype"].Value.ToString();
Isinsert = false;
}
else if (dgvwDtls.Columns[e.ColumnIndex].Name == "Delete" && e.RowIndex >= 0)
{
bnkid = Convert.ToInt32(dgvwDtls.Rows[e.RowIndex].Cells["bankid"].Value);
DialogResult dr = MessageBox.Show("Are you sure to delete row?", "Confirmation", MessageBoxButtons.YesNo);
if (dr == DialogResult.Yes)
{
bnkinfo.DeleteData(bnkid);
}
else if (DialogResult == DialogResult.No)
{
MessageBox.Show("Nothing Deleted");
}
LoadDataGridView();
}
}</blockquote
推荐答案
检查:
e.RowIndex >= 0
您可以使用列的 .SortMode
属性,请参阅:
如何:在Windows窗体DataGridView控件中设置列的排序模式 [ ^ ]
另外请参阅:在列标题单击上对datagridview进行排序[ ^ ]
You can use the .SortMode
property for columns, see:
How to: Set the Sort Modes for Columns in the Windows Forms DataGridView Control[^]
Also see: sort datagridview on column header click[^]
RickZeela是对的。我发布了第二个解决方案,稍微修正了您显示的代码:
RickZeeland is right. I post this second solution with a slight correction from the code you showed:
private void dgvwDtls_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex > -1) {
ChqMngrBsns.Masters.BankInfo bnkinfo = new ChqMngrBsns.Masters.BankInfo();
bnkid = Convert.ToInt32(dgvwDtls.Rows[e.RowIndex].Cells["bankid"].Value);
if (dgvwDtls.Columns[e.ColumnIndex].Name == "Edit")
{
txtBnknm.Text = dgvwDtls.Rows[e.RowIndex].Cells["bankname"].Value.ToString();
string CTS = dgvwDtls.Rows[e.RowIndex].Cells["Chequetype"].Value.ToString();
if (CTS == "True")
{
rbchqtype1.Checked = true;
}
else if (CTS == "False")
{
rbchqtype2.Checked = true;
}
txtAcntno.Text = dgvwDtls.Rows[e.RowIndex].Cells["Accountno"].Value.ToString();
cmbAcntype.Text = dgvwDtls.Rows[e.RowIndex].Cells["Accounttype"].Value.ToString();
Isinsert = false;
}
else if (dgvwDtls.Columns[e.ColumnIndex].Name == "Delete")
{
bnkid = Convert.ToInt32(dgvwDtls.Rows[e.RowIndex].Cells["bankid"].Value);
DialogResult dr = MessageBox.Show("Are you sure to delete row?", "Confirmation", MessageBoxButtons.YesNo);
if (dr == DialogResult.Yes)
{
bnkinfo.DeleteData(bnkid);
}
else if (DialogResult == DialogResult.No)
{
MessageBox.Show("Nothing Deleted");
}
LoadDataGridView();
}
}
}
如果您将RowIndex的测试放在开头,则可以提前退出该方法。但是,当您单击列标题时,会触发CellContentClick事件。
If you place the test of the RowIndex at the beginning, you get an early exit of the method. Strange that the CellContentClick event is fired when you click on a column header, though.
这篇关于如何在C#windows应用程序中单击列标题时对datagridview数据进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!