我想 DataGridView 只显示数据(readonly),用户无法直接通过datagridview输入数据,而是通过 DataSource 和 DataMember < / strong>即可。为了防止用户输入数据,我确实设置了:
m_dView.AllowUserToAddRows = false;
为什么设置后,DataGridView Auto Scroll不起作用?
m_dView.DataSource = ds;
m_dView.DataMember = dm;
m_dView.AutoGenerateColumns = true;
m_dView.MultiSelect = false;
m_dView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
m_dView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
m_dView.ReadOnly = true;
m_dView.AllowUserToAddRows = false;
m_dView.AllowUserToDeleteRows = false;
m_dView.AllowUserToOrderColumns = false;
m_dView.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
m_dView.AllowUserToResizeColumns = false;
m_dView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
m_dView.AllowUserToResizeRows = false;
m_dView.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;
// auto scroll only worked only when m_dView.AllowUserToAddRows = true;
if (m_dView.Rows.Count > 0) {
m_dView.CurrentCell = m_dView.Rows[m_dView.Rows.Count - 1].Cells[0];
}
答案 0 :(得分:1)
我创建了一个代码示例,如下所示,它完美无缺。 在新表单上拖放datadridview控件,如下所示,并将其命名为m_dView
创建了一个按钮,单击该按钮时,我运行下面的代码示例
using (SqlConnection c = new SqlConnection("Server=localhost;Database=deno;Trusted_Connection=True;"))
{
c.Open();
using (SqlDataAdapter a = new SqlDataAdapter(
"SELECT * FROM test", c))
{
// 3
// Use DataAdapter to fill DataTable
string dm = "TestScroll";
DataSet ds = new DataSet();
a.Fill(ds, dm);
// 4
// Render data onto the screen
m_dView.AllowUserToAddRows = false;
m_dView.DataSource = ds;
m_dView.DataMember = dm;
m_dView.AutoGenerateColumns = true;
m_dView.MultiSelect = false;
m_dView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
m_dView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
m_dView.ReadOnly = true;
m_dView.AllowUserToAddRows = false;
m_dView.AllowUserToDeleteRows = false;
m_dView.AllowUserToOrderColumns = false;
m_dView.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
m_dView.AllowUserToResizeColumns = false;
m_dView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
m_dView.AllowUserToResizeRows = false;
m_dView.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;
}
m_dView.FirstDisplayedScrollingRowIndex = m_dView.Rows.Count-1;
}
我已使用DataGridView.FirstDisplayedScrollingRowIndex属性
将gridview滚动到最后一行例如
m_dView.FirstDisplayedScrollingRowIndex = m_dView.Rows.Count-1;
您可以将以下代码复制并粘贴到新项目并进行测试吗?这可能是其他一些问题,真正的罪魁祸首。