PDA

View Full Version : سوال: خطا در قابلیت Select , Delete , Update در GridView



amir4015
پنج شنبه 14 آذر 1392, 13:06 عصر
با سلام
من GridView رو به وسیله کد نویسی مقدار دهی کردم و قابلیت Select , Delete , Update رو هم فعال کردم در خصوصیت RowCommand کدهای زیر رو نوشتم اما ارور زیرو می ده

protected void grd_ViewCategory_d_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Select" || e.CommandName == "Updated")
{
int index = Convert.ToInt32(e.CommandArgument);
try
{

int category_total = 0;


SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrin gs["conn"].ConnectionString.ToString());
string query = "SELECT subcategory FROM tbl_subcategory where category=@category";
SqlCommand cmd = new SqlCommand(query, con);
con.Open();
// cmd.Parameters.AddWithValue(@"category", DDL1.SelectedItem.ToString());
cmd.Parameters.AddWithValue(@"category", grd_ViewCategory_d.Rows[index].Cells[1].Text);
object obj = cmd.ExecuteScalar();
SqlDataReader redere;
redere = cmd.ExecuteReader();
while (redere.Read())
{
category_total++;
}
redere.Close();

con.Close();
uppi(grd_ViewCategory_d.Rows[index].Cells[1].Text, category_total );



grd_ViewCategory_d.DataBind();
}
catch (Exception ex)
{

}
}
}


113511

113512

amir4015
پنج شنبه 14 آذر 1392, 13:09 عصر
این هم گریدویو

<asp:GridView ID="grd_ViewBookCategory_d" runat="server" AllowPaging="True"
AutoGenerateColumns="False"
Width="680px" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None"
BorderWidth="1px" CellPadding="3"
onrowcommand="grd_ViewBookCategory_d_RowCommand">
<AlternatingRowStyle BackColor="#0099CC" ForeColor="White" />
<Columns>
<asp:BoundField DataField="id_book" HeaderText="ردیف" InsertVisible="False"
ReadOnly="True" SortExpression="id_book" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="category" HeaderText="مقطع تحصیلی"
SortExpression="category" />
<asp:BoundField DataField="subcategory" HeaderText="رشته تحصیلی"
SortExpression="subcategory" />
<asp:BoundField DataField="book" HeaderText="عنوان کتاب"
SortExpression="book" />
<asp:BoundField DataField="book_total" HeaderText="تعداد سوالات"
SortExpression="book_total" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:CommandField ButtonType="Image" CancelImageUrl="~/file/image/cancel.png"
DeleteImageUrl="~/file/image/delete.png" EditImageUrl="~/file/image/edit.png"
HeaderText="ابزار" SelectImageUrl="~/file/image/accept.png"
ShowDeleteButton="True" ShowEditButton="True" ShowSelectButton="True"
UpdateImageUrl="~/file/image/refresh.png">
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
</asp:CommandField>
</Columns>
<EmptyDataTemplate>
رکوردی برای نمایش وجود ندارد
</EmptyDataTemplate>
<FooterStyle BackColor="White" ForeColor="#000066" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="False" Font-Italic="False"
ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#007DBB" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#00547E" />
</asp:GridView>

anvar
پنج شنبه 14 آذر 1392, 13:52 عصر
باید رویداد RowEditing رو هندل کنی تا اتفاقی بیافته


mygrid.editindex = e.neweditindex;

mygrid.datasource = ????;
mygrid.databind();

amir4015
پنج شنبه 14 آذر 1392, 14:23 عصر
مهندس پس این کدهایی که درون RowCommand نوشتم چی

anvar
پنج شنبه 14 آذر 1392, 16:37 عصر
شما رویداد RowEditing رو اضافه کردید یا نه؟

زمانی که دستوری مثل ویرایش رو پیاده سازی می کنید باید برای گرید ویو مشخص بشه که چه اتفاقی در حال افتادنه. مثلا با کلیک دکمه ویرایش ردیف مورد نظر به حالت ویرایش بره (RowEditing ) و کنترل هایی که در حالت edit item style برای گرید گذاشتی نمایش داده بشن - بعد کاربر ممکنه از ویرایش منصرف بشه (RowCancelingEdit) و یا ویرایش مورد نظرش رو انجام بده و رکورد مورد نظر رو آپدیت یا بروزرسانی بکنه (RowUpdating). باید هر کدوم از رویدادهای فوق رو هندل کنی تا کدهات بدرستی عمل کنن

amir4015
پنج شنبه 14 آذر 1392, 17:20 عصر
ممنون که جواب می دید
من رویداد رو اضافه کردم دیگه ارو نداد اما اتفاقی هم نیوفتاد و ردیف مورد نظر به حالت ویرایش نمی ره و تازهبار دوم که روی edit کلیک می کنم صفحه ارور می ده

amir4015
پنج شنبه 14 آذر 1392, 18:53 عصر
فایل برنامه

anvar
پنج شنبه 14 آذر 1392, 21:22 عصر
متاسفانه خیلی بد نوشتی :

کدها رو مستقیم کپی نکن - چون کامل ننوشتم هدفم فقط نشان دادن ویرایش و بروز رسانی بود. کدها تست شده هستن و مشکلی ندارن
در طراحی پایگاه داده ها سعی کن از ایندکس های عددی استفاده کنی - مثلا در جدول گروه ها یک ستون جدید بنام آی دی گروه اضافه کن و انتخاب ها رو بر اساس اون انجام بده نه خود گروه که فیلد متنیه. به کوئری آپدیت هم نیگا کن من حس می کنم که چیزی کم یا اضافه داشته باشه




کد اچ تی ام ال

<div>
<asp:DropDownList ID="DDL_category_d" runat="server" CssClass="font"
DataTextField="category"
DataValueField="category" AutoPostBack="True" OnSelectedIndexChanged="DDL_category_d_SelectedIndexChanged">
</asp:DropDownList>
</div>
<asp:GridView ID="grd_ViewCategory_d" runat="server" AutoGenerateColumns="False"

BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="0px"
CellPadding="3" Width="680px" AllowPaging="True"
EnableTheming="True" OnRowCancelingEdit="grd_ViewCategory_d_RowCancelingEdit" OnRowEditing="grd_ViewCategory_d_RowEditing" OnRowUpdating="grd_ViewCategory_d_RowUpdating">
<AlternatingRowStyle BackColor="#0099CC" BorderStyle="None" ForeColor="White" />
<Columns>
<asp:BoundField DataField="id_subcategory" HeaderText="ردیف"
SortExpression="id_subcategory" InsertVisible="False" ReadOnly="True" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="category" ReadOnly="true" HeaderText="مقطع تحصیلی"
SortExpression="category" />
<asp:BoundField DataField="subcategory" HeaderText="گرایش تحصیلی"
SortExpression="subcategory" />

<asp:TemplateField HeaderText="تعداد دروس">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("subcategory_total") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtsubcategory_total" Text='<%# Bind("subcategory_total") %>' runat="server"></asp:TextBox>
</EditItemTemplate>

</asp:TemplateField>
<asp:CommandField HeaderText="ابزار" ShowDeleteButton="True" ButtonType="Image"
CancelText="انصراف"
DeleteText="حذف"
EditText="ویرایش"
SelectText="انتخاب"
ShowEditButton="True" ShowSelectButton="True"
UpdateText="به روز رسانی">
<ItemStyle HorizontalAlign="Center" Width="100px" />
</asp:CommandField>
</Columns>
<EmptyDataTemplate>
<asp:Label ID="lbl_none" runat="server" Text="رکوردی برای نمایش وجود ندارد ."></asp:Label>
</EmptyDataTemplate>
<FooterStyle BackColor="White" ForeColor="#000066" BorderStyle="None" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White"
BorderStyle="None" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left"
BorderStyle="None" Wrap="False" />
<RowStyle ForeColor="#000066" BorderStyle="None" Wrap="True" />
<SelectedRowStyle BackColor="#669999" Font-Bold="False" ForeColor="White"
BorderStyle="None" Wrap="True" />
<SortedAscendingCellStyle BackColor="#F1F1F1" BorderStyle="None" />
<SortedAscendingHeaderStyle BackColor="#007DBB" BorderStyle="None" />
<SortedDescendingCellStyle BackColor="#CAC9C9" BorderStyle="None" />
<SortedDescendingHeaderStyle BackColor="#00547E" BorderStyle="None" />
</asp:GridView>
<asp:Label ID="lblerror" runat="server" Text="Label"></asp:Label>

کد سی شارپ


protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DDL_category_d.Items.Clear();

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrin gs["conn"].ConnectionString.ToString());
string query = "SELECT category FROM tbl_category";
SqlCommand cmd = new SqlCommand(query, con);
con.Open();

SqlDataReader redere;
redere = cmd.ExecuteReader();
while (redere.Read())
{
DDL_category_d.Items.Add(redere["category"].ToString());
}
redere.Close();

con.Close();
}
}

protected void grii()
{
string query = "SELECT * from tbl_subcategory where category=@category ORDER BY id_subcategory DESC";

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrin gs["conn"].ConnectionString.ToString());
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue(@"category", DDL_category_d.SelectedItem.Text);
con.Open();
// object obj = cmd.ExecuteScalar();

SqlDataAdapter da = null;
DataSet ds = null;
da = new SqlDataAdapter(cmd);
ds = new DataSet();
da.Fill(ds);
grd_ViewCategory_d.DataSource = ds.Tables[0];
grd_ViewCategory_d.DataBind();
con.Close();
}
protected void DDL_category_d_SelectedIndexChanged(object sender, EventArgs e)
{
grii();
}



protected void grd_ViewCategory_d_RowEditing(object sender, GridViewEditEventArgs e)
{
grd_ViewCategory_d.EditIndex = e.NewEditIndex;
// Set status message
lblerror.Text = "Editing row # " + e.NewEditIndex.ToString();
// Reload the grid
grii();
}
protected void grd_ViewCategory_d_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
// Cancel edit mode
grd_ViewCategory_d.EditIndex = -1;
// Set status message
lblerror.Text = "Editing canceled";
// Reload the grid
grii();
}
protected void grd_ViewCategory_d_RowUpdating(object sender, GridViewUpdateEventArgs e)
{


int index = Convert.ToInt32(e.RowIndex);

int category_total = Int32.Parse(((TextBox)grd_ViewCategory_d.Rows[e.RowIndex].FindControl("txtsubcategory_total")).Text);


try
{
string query = "update tbl_subcategory set subcategory_total=@category_total where category=@category ";
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrin gs["conn"].ConnectionString.ToString());
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue(@"category_total", category_total);
cmd.Parameters.AddWithValue(@"category", grd_ViewCategory_d.Rows[index].Cells[1].Text);
con.Open();

cmd.ExecuteNonQuery();

con.Close();
lblerror.Text = "با موافقیت تغییرات ذخیره شد";
//System.Threading.Thread.Sleep(3000);



// Cancel edit mode
grd_ViewCategory_d.EditIndex = -1;
grii();
}
catch (Exception ex)
{
lblerror.Text = " سیستم با مشکل مواجع شده است دوباره تلاش کنید " + ex.Message;
}
}