|
■No63633 (はっちゃん さん) に返信
> ■No63632 (はっちゃん さん) に返信
csファイル続きです。
<DataGrid.aspx.cs>
protected void btnFix_Click(object sender, EventArgs e)
{
bool IsFix = false;
SqlConnection conn = null;
SqlDataAdapter adp = null;
SqlCommandBuilder cb = null;
foreach (DataRow dr in _subDt.Rows)
{
if (dr.RowState != DataRowState.Deleted)
{
int index = 0;
foreach (DataGridItem dgi in MyGrid.Items)
{
if (((TextBox)dgi.FindControl("txtSorts")).Text == dr["Sorts"].ToString())
{
index = dgi.ItemIndex;
break;
}
}
TextBox txtProductName = (TextBox)MyGrid.Items[index].FindControl("txtProductName");
TextBox txtUnitPrice = (TextBox)MyGrid.Items[index].FindControl("txtUnitPrice");
if (txtProductName != null && dr["ProductName"].ToString() != txtProductName.Text)
{
dr["ProductName"] = txtProductName.Text;
}
if (txtUnitPrice != null && dr["UnitPrice"].ToString() != txtUnitPrice.Text)
{
if (txtUnitPrice.Text != "")
{
dr["UnitPrice"] = decimal.Parse(txtUnitPrice.Text);
}
else
{
dr["UnitPrice"] = DBNull.Value;
}
}
if (txtProductName.Text == "" && txtUnitPrice.Text != "")
{
MsgBox(Page, "商品名が入力されていません。");
Session["IsErrProductName"] = true;
Session["IsErrItemIndex"] = index;
BindMyGrid(_subDt);
return;
}
else if (txtProductName.Text != "" && txtUnitPrice.Text == "")
{
MsgBox(Page, "単価が入力されていません。");
Session["IsErrUnitPrice"] = true;
Session["IsErrItemIndex"] = index;
BindMyGrid(_subDt);
return;
}
}
}
DataTable dt = _subDt.GetChanges();
if (dt != null && dt.Rows.Count > 0)
{
for (int i = dt.Rows.Count; i > 0; i--)
{
if (dt.Rows[i - 1].RowState != DataRowState.Deleted)
{
if (dt.Rows[i - 1]["ProductName"].ToString() == "" && dt.Rows[i - 1]["UnitPrice"].ToString() == "")
{
dt.Rows[i - 1].Delete();
}
}
}
if (dt.Rows.Count == 0)
{
dt = null;
}
}
string strSQL = "select * from T_T_M商品";
try
{
using (conn = new SqlConnection(strConStr))
{
using (adp = new SqlDataAdapter(strSQL, conn))
{
using (cb = new SqlCommandBuilder(adp))
{
if (dt != null)
{
adp.Update(dt);
IsFix = true;
}
}
}
}
btnRefresh_Click(sender, e);
if (IsFix)
{
MsgBox(Page, "正常終了!!");
}
else
{
MsgBox(Page, "データの変更はありません。");
}
}
catch (Exception ex)
{
MsgBox(Page, "エラーが発生しました:" + ex.Message);
}
finally
{
if (dt != null)
{
dt.Dispose();
}
if (cb != null)
{
cb.Dispose();
}
if (adp != null)
{
adp.Dispose();
}
if (conn != null)
{
conn.Dispose();
}
}
}
protected void MyGrid_PageIndexChanged(object sender, DataGridPageChangedEventArgs e)
{
MyGrid.CurrentPageIndex = e.NewPageIndex;
BindMyGrid(_subDt);
}
protected void MyGrid_ItemCreated(object sender, DataGridItemEventArgs e)
{
DataGridItem dataItem = e.Item;
ListItemType lit = dataItem.ItemType;
switch (lit)
{
case ListItemType.Header:
dataItem.HorizontalAlign = HorizontalAlign.Center;
dataItem.ForeColor = Color.White;
dataItem.BackColor = Color.DarkSlateGray;
dataItem.Font.Bold = true;
break;
case ListItemType.Pager:
dataItem.BackColor = Color.LightGray;
break;
case ListItemType.Item:
dataItem.BackColor = Color.White;
break;
case ListItemType.AlternatingItem:
dataItem.BackColor = Color.LightCyan;
break;
case ListItemType.EditItem:
dataItem.BackColor = Color.Magenta;
break;
case ListItemType.SelectedItem:
dataItem.BackColor = Color.Yellow;
break;
case ListItemType.Footer:
dataItem.BackColor = Color.DarkSlateGray;
int iC = dataItem.Cells.Count;
while (dataItem.Cells.Count > 1)
{
dataItem.Cells.RemoveAt(1);
}
dataItem.Cells[0].ColumnSpan = iC;
break;
}
}
protected void MyGrid_ItemCommand(object sender, DataGridCommandEventArgs e)
{
if (e.CommandName == "insert")
{
Insert();
Session["IsNewRow"] = true;
BindMyGrid(_subDt);
}
}
private bool IsNumeric(string strText)
{
decimal dTmp;
return decimal.TryParse(strText, out dTmp);
}
protected void MyGrid_ItemDataBound(object sender, DataGridItemEventArgs e)
{
Button btnDelete;
DataGridItem dataItem = e.Item;
ListItemType lit = dataItem.ItemType;
if (lit == ListItemType.Item || lit == ListItemType.AlternatingItem)
{
btnDelete = (Button)dataItem.Cells[3].Controls[0];
btnDelete.Attributes["onClick"] = "return confirm('この行を削除します。よろしいですか?');";
btnDelete.ForeColor = Color.Blue;
TextBox txtProductName = (TextBox)dataItem.FindControl("txtProductName");
if ((bool)Session["IsNewRow"] && !(bool)Session["IsErrProductName"] && !(bool)Session["IsErrUnitPrice"])
{
ScriptManager1.SetFocus(txtProductName);
}
if ((int)Session["IsErrItemIndex"] == dataItem.ItemIndex)
{
if ((bool)Session["IsErrProductName"] && txtProductName.Text == "")
{
txtProductName.BackColor = Color.Yellow;
ScriptManager1.SetFocus(txtProductName);
}
TextBox txtUnitPrice = (TextBox)dataItem.FindControl("txtUnitPrice");
if ((bool)Session["IsErrUnitPrice"] && txtUnitPrice.Text == "")
{
txtUnitPrice.BackColor = Color.Yellow;
ScriptManager1.SetFocus(txtUnitPrice);
}
}
}
else if (lit == ListItemType.Footer)
{
Button btnNewAddRow = new Button();
btnNewAddRow = (Button)dataItem.FindControl("btnNewAddRow");
btnNewAddRow.ForeColor = Color.Red;
btnDelete = (Button)dataItem.FindControl("btnDelete");
btnDelete.Attributes["onClick"] = "return confirm('この行を削除します。よろしいですか?');";
if (MyGrid.Items.Count <= 0)
{
btnDelete.Enabled = false;
}
else
{
btnDelete.Enabled = true;
}
}
}
|