Vb.net DataGridView验证:如何确保行的每个单元格都包含一个值?
我的表单上有一个Vb.net DataGridView验证:如何确保行的每个单元格都包含一个值?,vb.net,validation,datagridview,Vb.net,Validation,Datagridview,我的表单上有一个DataGridView控件。它有6列6行(永远不会改变)。当用户在任何列下的单元格中输入数据时,我希望确保他们填写该行的其余单元格。所以基本上,如果他们把数据放在第0行–第0列,我想确保第0行–第1列,第0行–第2列等等。。。里面有数据。在将其提交到数据库之前,出于验证原因,我需要此文件。如果该行的字段没有全部填写,我想显示一条消息,其中包含需要修复的行 非常感谢您的帮助 这里有一个更新,我已经知道需要做什么了 Private Sub ValidateYear() D
DataGridView
控件。它有6列6行(永远不会改变)。当用户在任何列下的单元格中输入数据时,我希望确保他们填写该行的其余单元格。所以基本上,如果他们把数据放在第0行–第0列,我想确保第0行–第1列,第0行–第2列等等。。。里面有数据。在将其提交到数据库之前,出于验证原因,我需要此文件。如果该行的字段没有全部填写,我想显示一条消息,其中包含需要修复的行
非常感谢您的帮助
这里有一个更新,我已经知道需要做什么了
Private Sub ValidateYear()
Dim oInvYear As New Collection
Dim oErrorMsg As New System.Text.StringBuilder
Dim blnErrFound As Boolean = False
'Loop through year column and check for number, if blank skip'
For i As Integer = 0 To dgvIntervals.Rows.Count - 1
If Not String.IsNullOrEmpty(dgvIntervals.Rows(i).Cells(4).Value) Then
If Not IsNumeric(dgvIntervals.Rows(i).Cells(4).Value) Then
oInvYear.Add(i + 1)
blnErrFound = True
End If
End If
Next
'If errors found, lets append them to our message'
If blnErrFound Then
oErrorMsg.Append("PLEASE FIX ERRORS BELOW BEFORE PROCEEDING")
oErrorMsg.AppendLine("")
oErrorMsg.Append(vbCrLf)
'Get our year count errors'
If oInvYear.Count > 0 Then
oErrorMsg.Append("* Year must be a number- ")
oErrorMsg.Append("Line(s): ")
For i As Integer = 1 To oInvYear.Count
If i >= 2 Then
oErrorMsg.Append(", ")
End If
oErrorMsg.Append(oInvYear.Item(i).ToString)
Next
oErrorMsg.Append(vbCrLf)
End If
'Show them to our user'
MsgBox(oErrorMsg.ToString)
End Sub
在这个场景中,我建议您读取每一行中的每一个单元格,当您在任何单元格中找到一个值时,您需要确保其他单元格都有值 我为你做了一个小样本,我希望这能帮助你解决你的问题 首先,我创建此实体以填充我的gridview:
public class MyEntity
{
public string ID { get; set; }
public string Name { get; set; }
public string LastName { get; set; }
}
这是我的aspx页面上的代码
<form id="form1" runat="server">
<div>
<asp:GridView ID="grvData" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="ID" >
<ItemTemplate >
<asp:Label ID="lblID" runat="server" Text='<%# Eval("ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:TextBox ID="txtName" runat="server" Text='<%# Eval("Name") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name">
<ItemTemplate>
<asp:TextBox ID="txtLastName" runat="server" Text='<%# Eval("LastName") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<asp:Button ID="btnValidate" runat="server" Text="Validate" OnClick="btnValidate_Click" />
<br />
<asp:Label ID="lblMessage" runat="server" ForeColor="Red" Text="">
</asp:Label>
</div>
</form>
然后在我的代码后面
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
List<MyEntity> data = GenerateData();
this.grvData.DataSource = data;
this.grvData.DataBind();
}
}
protected void btnValidate_Click(object sender, EventArgs e)
{
int columns = this.grvData.Columns.Count - 1;
foreach (GridViewRow row in this.grvData.Rows)
{
int count = columns;
TextBox tbName = row.Cells[1].FindControl("txtName") as TextBox;
TextBox tbLastName = row.Cells[2].FindControl("txtLastName") as TextBox;
if (!string.IsNullOrWhiteSpace(tbName.Text))
{
count--;
}
if (!string.IsNullOrWhiteSpace(tbLastName.Text))
{
count--;
}
if (count != columns && count != 0)
{
this.lblMessage.Text = "Invalid input, you need to supply data for every field.";
break;
}
}
}
private List<MyEntity> GenerateData()
{
List<MyEntity> list = new List<MyEntity>();
for (int i = 0; i < 5; i++)
{
MyEntity entity = new MyEntity() { ID = Guid.NewGuid().ToString() };
list.Add(entity);
}
return list;
}
}
public部分类默认值:System.Web.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{
如果(!IsPostBack)
{
列表数据=GenerateData();
this.grvData.DataSource=数据;
this.grvData.DataBind();
}
}
受保护的无效btnValidate\u单击(对象发送者,事件参数e)
{
int columns=this.grvData.columns.Count-1;
foreach(此.grvData.Rows中的GridViewRow行)
{
int count=列;
TextBox tbName=行。单元格[1]。FindControl(“txtName”)作为TextBox;
TextBox tbLastName=row.Cells[2]。FindControl(“txtLastName”)作为TextBox;
如果(!string.IsNullOrWhiteSpace(tbName.Text))
{
计数--;
}
如果(!string.IsNullOrWhiteSpace(tbLastName.Text))
{
计数--;
}
if(count!=列&&count!=0)
{
this.lblMessage.Text=“输入无效,您需要为每个字段提供数据。”;
打破
}
}
}
私有列表生成数据()
{
列表=新列表();
对于(int i=0;i<5;i++)
{
MyEntity entity=new MyEntity(){ID=Guid.NewGuid().ToString()};
列表。添加(实体);
}
退货清单;
}
}
正如您所看到的,这非常简单,如果您加载的记录太多且列太多,我不建议您使用这种方法,因为这可能会影响性能,但在您的情况下,我认为这应该是可行的
另外,我的答案是使用visual C#,因为当我阅读它时,你没有提到语言,现在你只是更改了它。使用DataGridView控件的CellValidating或RowValidating事件来验证用户输入的数据
Private Sub OnRowValidating(ByVal sender As Object, ByVal args As DataGridViewCellCancelEventArgs) Handles DataGridView1.RowValidating
Dim row As DataGridViewRow = DataGridView1.Rows(args.RowIndex)
For Each cell As DataGridViewCell In row.Cells
If String.IsNullOrEmpty(cell.Value.ToString()) Then
'show a message box or whatever...
End If
Next
End Sub
非主题推荐:您的代码中有一些VB6样式的元素,需要引用Microsoft.VisualBasic程序集。要消除这种情况,请使用.NET的
System.Environment.NewLine
替换例如vbCrLf
,或将MsgBox()
替换为System.Windows.Forms.MessageBox.Show()
。