gpt4 book ai didi

c# - 是否可以在 C# 中的 gridview 上进行验证?

转载 作者:太空宇宙 更新时间:2023-11-03 13:18:03 24 4
gpt4 key购买 nike

我有一个具有插入功能的 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();
}

最佳答案

每次添加新客户时,您都在执行插入操作之前绑定(bind) GridView。。这种重新绑定(bind)每次都会清除您的值。

要么删除 AddNewCustomer 中的绑定(bind)(因为无论如何您都会在 ValidateAdd 中再次绑定(bind)),或者删除 ValidateAdd 中的绑定(bind)并将绑定(bind)移动到AddNewCustomer 插入之后。

关于c# - 是否可以在 C# 中的 gridview 上进行验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25384828/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com