3个回答
展开全部
你可以使用datagrid控件,他是具有这种功能的。
添加,删除,修改基本上都是datagridview自动完成的,保存数据要自己弄一下,看你的数据与是怎么样的
public partial class TeamManagement : UserControl
{
public TeamManagement()
{
InitializeComponent();
}
private void TeamManagement_Load(object sender, EventArgs e)
{
RefreshGrid();
}
private void RefreshGrid()
{
teamTableAdapter.Fill(roadFeeDataSet.Team);
dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
dataGridView.AutoResizeColumns();
}
private void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
rowUpdated = true;
if (dataGridView.Columns[e.ColumnIndex].Name == "teamNameDataGridViewTextBoxColumn")
{
foreach (DataGridViewRow row in dataGridView.Rows)
{
if (e.RowIndex != row.Index && !row.IsNewRow &&
dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null &&
row.Cells[e.ColumnIndex].Value != null &&
e.FormattedValue.ToString().Trim()
== row.Cells[e.ColumnIndex].Value.ToString().Trim()
)
{
MessageBox.Show("名称已存在,请使用不同的名称!", "输入错误",
MessageBoxButtons.OK, MessageBoxIcon.Error);
e.Cancel = true;
}
}
}
}
private void dataGridView_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
{
if ((int)userTableAdapter.SelectCountByTeamID((int?)e.Row.Cells["teamIDDataGridViewTextBoxColumn"].Value) > 0)
{
MessageBox.Show("此记录已在使用,您不能删除!", "提示", MessageBoxButtons.OK,
MessageBoxIcon.Information);
e.Cancel = true;
return;
}
if (MessageBox.Show("您真的要删除此条记录吗?", "提示", MessageBoxButtons.YesNo,
MessageBoxIcon.Question) == DialogResult.No)
{
e.Cancel = true;
}
}
private bool rowAdded = false;
private bool rowUpdated = false;
private void dataGridView_UserAddedRow(object sender, DataGridViewRowEventArgs e)
{
rowAdded = true;
}
private void dataGridView_RowValidated(object sender, DataGridViewCellEventArgs e)
{
if (rowAdded || rowUpdated)
{
rowAdded = false;
rowUpdated = false;
teamTableAdapter.Update(roadFeeDataSet);
RefreshGrid();
}
}
private void dataGridView_UserDeletedRow(object sender, DataGridViewRowEventArgs e)
{
teamTableAdapter.Update(roadFeeDataSet);
RefreshGrid();
}
}
添加,删除,修改基本上都是datagridview自动完成的,保存数据要自己弄一下,看你的数据与是怎么样的
public partial class TeamManagement : UserControl
{
public TeamManagement()
{
InitializeComponent();
}
private void TeamManagement_Load(object sender, EventArgs e)
{
RefreshGrid();
}
private void RefreshGrid()
{
teamTableAdapter.Fill(roadFeeDataSet.Team);
dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
dataGridView.AutoResizeColumns();
}
private void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
rowUpdated = true;
if (dataGridView.Columns[e.ColumnIndex].Name == "teamNameDataGridViewTextBoxColumn")
{
foreach (DataGridViewRow row in dataGridView.Rows)
{
if (e.RowIndex != row.Index && !row.IsNewRow &&
dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null &&
row.Cells[e.ColumnIndex].Value != null &&
e.FormattedValue.ToString().Trim()
== row.Cells[e.ColumnIndex].Value.ToString().Trim()
)
{
MessageBox.Show("名称已存在,请使用不同的名称!", "输入错误",
MessageBoxButtons.OK, MessageBoxIcon.Error);
e.Cancel = true;
}
}
}
}
private void dataGridView_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
{
if ((int)userTableAdapter.SelectCountByTeamID((int?)e.Row.Cells["teamIDDataGridViewTextBoxColumn"].Value) > 0)
{
MessageBox.Show("此记录已在使用,您不能删除!", "提示", MessageBoxButtons.OK,
MessageBoxIcon.Information);
e.Cancel = true;
return;
}
if (MessageBox.Show("您真的要删除此条记录吗?", "提示", MessageBoxButtons.YesNo,
MessageBoxIcon.Question) == DialogResult.No)
{
e.Cancel = true;
}
}
private bool rowAdded = false;
private bool rowUpdated = false;
private void dataGridView_UserAddedRow(object sender, DataGridViewRowEventArgs e)
{
rowAdded = true;
}
private void dataGridView_RowValidated(object sender, DataGridViewCellEventArgs e)
{
if (rowAdded || rowUpdated)
{
rowAdded = false;
rowUpdated = false;
teamTableAdapter.Update(roadFeeDataSet);
RefreshGrid();
}
}
private void dataGridView_UserDeletedRow(object sender, DataGridViewRowEventArgs e)
{
teamTableAdapter.Update(roadFeeDataSet);
RefreshGrid();
}
}
追问
dataGridView有增加功能,asp.net的GridView没有自带增加的功能吧?
追答
没有增加功能,只有删除和修改。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
sql中有个bulk功能。你查看一下吧。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我在CSDN上写了一篇文章你可以参考一下,就是你不知道你能不能看到,呵呵!
blog.csdn.net/chuxue1342/archive/2010/11/11/6002880.aspx
blog.csdn.net/chuxue1342/archive/2010/11/11/6002880.aspx
追问
看了你的博客,收获不小。本人新手,还得消化消化。O(∩_∩)O。
能用asp.net的gridview直接实现吗?
追答
其实只要稍微转下是可以的,修改那个方法我故意换成DataSet这样比较方便,如果你的GridView.DataSource绑定的是:DataTable的话,你可以这样:
DataSet ds=New DataSet();
DataTable table=(DataTable)GridView.DataSource;
ds.Tables.Add(table);
然后就可以调用了!
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询