可以在c#中验证gridview吗?
本文关键字:gridview 验证 | 更新日期: 2023-09-27 18:18:51
我有一个具有插入功能的gridview,我试图根据有行数限制insert语句。目前设置的问题是,当我输入新行时,验证检查有效,但行不包含任何数据,因此将空行插入gridview。
当我在调试器中执行代码时,局部变量都显示为""
,即使我在按提交之前输入了数据。
当我在没有任何验证的情况下将代码块移动到AddNewCustomer时,它会正常工作。我试着这样做:
(MGRchk设置为= datatable.rows.count)
private void BindData()
{
string strQuery = "SELECT Person.PersonID, Person.FirstName AS FirstName, Person.LastName AS LastName, Person.Emergency# AS Emergency#, Person.Cell# AS Cell#, Person.Age AS Age, " +
"Person.ActivityCard AS ActivityCard, Person.CraftType AS CraftType, Person.Initials AS Initials, Person.Group# AS Group# " +
"FROM Person INNER JOIN " +
"TripSchedule ON Person.PersonID = TripSchedule.PersonID INNER JOIN Bus ON TripSchedule.BusID = Bus.BusID where TripSchedule.Date = '" + TextBox1.Text + "' and " +
"TripSchedule.Time = '" + ddlTripTime.SelectedItem + "' and TripSchedule.TripType = '" + ddlTripType.SelectedItem + "' and Bus.Bus# = '" + ddlBus.SelectedItem + "' order by Person.Group#";
SqlCommand cmd = new SqlCommand(strQuery);
TRIPTYPE = ddlTripType.SelectedItem.ToString();
GridView1.DataSource = GetData(cmd);
GridView1.DataBind();
}
private DataTable GetData(SqlCommand cmd)
{
DataTable dt = new DataTable();
SqlConnection conn = new SqlConnection(strConnString);
SqlDataAdapter adapter = new SqlDataAdapter();
cmd.CommandType = CommandType.Text;
cmd.Connection = conn;
adapter.SelectCommand = cmd;
adapter.Fill(dt);
int dtr = 0;
dtr = dt.Rows.Count;
MGRchk = dtr;
if (dt.Rows.Count > 0)
{
Formats();
PrintTable(dt);
lblAct.Visible = false;
lblAgee.Visible = false;
lblBoats.Visible = false;
lblCells.Visible = false;
lblEmer.Visible = false;
lblFirst.Visible = false;
lblGroups.Visible = false;
lblInit.Visible = false;
lblLast.Visible = false;
txtAct.Visible = false;
ddlAgeR.Visible = false;
ddlBoat1.Visible = false;
txtCells.Visible = false;
txtEmer.Visible = false;
txtFirst.Visible = false;
txtGroups.Visible = false;
txtInit.Visible = false;
txtLast.Visible = false;
btnSubmitted.Visible = false;
}
else
{
lblAct.Visible = true;
lblAgee.Visible = true;
lblBoats.Visible = true;
lblCells.Visible = true;
lblEmer.Visible = true;
lblFirst.Visible = true;
lblGroups.Visible = true;
lblInit.Visible = true;
lblLast.Visible = true;
txtAct.Visible = true;
ddlAgeR.Visible = true;
ddlBoat1.Visible = true;
txtCells.Visible = true;
txtEmer.Visible = true;
txtFirst.Visible = true;
txtGroups.Visible = true;
txtInit.Visible = true;
txtLast.Visible = true;
btnSubmitted.Visible = true;
txtAct.Text = "";
txtCells.Text = "";
txtEmer.Text = "";
txtFirst.Text = "";
txtGroups.Text = "";
txtInit.Text = "";
txtLast.Text = "";
}
protected void AddNewCustomer(object sender, EventArgs e)
{
BindData();
switch(TRIP)
{
case "CANOE":
if(MGRchk < 24)
{
ValidatedAdd(sender, e)
break;
}
else
{
lblError1.Text = error1;
}
}
//etc
protected void ValidatedAdd(object sender, EventArgs e)
{
string nFirstName = ((TextBox)GridView1.FooterRow.FindControl("txtFirstName")).Text;
string nLastName = ((TextBox)GridView1.FooterRow.FindControl("txtLastName")).Text;
string nEmergency = ((TextBox)GridView1.FooterRow.FindControl("txtEmergency")).Text;
string nCell = ((TextBox)GridView1.FooterRow.FindControl("txtCell")).Text;
string nAge = ((TextBox)GridView1.FooterRow.FindControl("txtAge")).Text;
string nActivityCard = ((TextBox)GridView1.FooterRow.FindControl("txtActivityCard")).Text;
string nInitials = ((TextBox)GridView1.FooterRow.FindControl("txtInitials")).Text;
string nGroup = ((TextBox)GridView1.FooterRow.FindControl("txtGroup")).Text;
string aboat = ((DropDownList)GridView1.FooterRow.FindControl("ddlBoat")).Text;
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand("INSERT INTO Person(FirstName, LastName, Emergency#, Cell#, Age, ActivityCard, CraftType, Initials, Group#) " +
"values(@FirstName, @LastName, @Emergency, @Cell, @Age, @ActivityCard, @Boat, @Initials, @Group);"
+ "insert into TripSchedule(TripType, PersonID, Time, Date, BusID) values (@Type, SCOPE_IDENTITY(), @Time, @Date, @Bus);" +
"SELECT Person.PersonID, Person.FirstName AS FirstName, Person.LastName AS LastName, Person.Emergency# AS Emergency#, Person.Cell# AS Cell#, Person.Age AS Age, " +
"Person.ActivityCard AS ActivityCard, Person.CraftType AS CraftType, Person.Initials AS Initials, Person.Group# AS Group# " +
"FROM Person INNER JOIN " +
"TripSchedule ON Person.PersonID = TripSchedule.PersonID INNER JOIN Bus on Bus.BusID = TripSchedule.BusID where TripSchedule.Date = @Date and " +
"TripSchedule.Time = @Time and TripSchedule.TripType = @Type and Bus.BusID = @Bus order by Person.Group#;");
cmd.Parameters.Add("@FirstName", SqlDbType.VarChar).Value = nFirstName;
cmd.Parameters.Add("@LastName", SqlDbType.VarChar).Value = nLastName;
cmd.Parameters.Add("@Emergency", SqlDbType.NChar).Value = nEmergency;
cmd.Parameters.Add("@Cell", SqlDbType.NChar).Value = nCell;
cmd.Parameters.Add("@Age", SqlDbType.NChar).Value = nAge;
cmd.Parameters.Add("@ActivityCard", SqlDbType.NChar).Value = nActivityCard;
cmd.Parameters.Add("@Initials", SqlDbType.NChar).Value = nInitials;
cmd.Parameters.Add("@Group", SqlDbType.VarChar).Value = nGroup;
cmd.Parameters.AddWithValue("@Date", TextBox1.Text);
cmd.Parameters.AddWithValue("@Time", ddlTripTime.SelectedItem.ToString());
cmd.Parameters.AddWithValue("@Type", ddlTripType.SelectedItem.ToString());
cmd.Parameters.AddWithValue("@Boat", aboat.ToString());
cmd.Parameters.AddWithValue("@Bus", ddlBus.SelectedItem.ToString());
GridView1.DataSource = GetData(cmd);
GridView1.DataBind();
}
每次添加新客户时,在插入之前绑定GridView 。每次重新绑定都会清除你的值。
要么在AddNewCustomer
中删除该绑定(因为您无论如何都要在ValidateAdd
中再次绑定),要么在ValidateAdd
中删除绑定,并将AddNewCustomer
中的绑定移动到之后插入