PDA

View Full Version : استفاده ازlistbox و checkBox در gridview



anahita_s
سه شنبه 24 مهر 1386, 21:29 عصر
باسلام

من از gridview استفاده کردم، که در آن از دو templatefield استفاده کردم یکی شاملlistbox و دیگری checkBoxاست قرار دادم، حالا می خواهم که هر سطری که چک خورد اطلاعاتش قابل دسترسی باشد ولی کدی که نوشتم چک شدن checkbox را تشخیص نمی دهد، همچنین به مقدار انتخاب شده در listBox نیز نمی توان دسترسی داشت این کد را در onclick یک button قرار دادم. ولی با چک کردن checkboxها وارد شرط ifنمیشود.زمانیکه شرط if را بصورت (chkSelectCats.Checked!) تغییر می دهیم این شرط به ازای همه سطرهای اجرا می شود.( چه سطرهای چک خورده و چه سطرهای چک نخورده)


protectedSelectCategories(object sender, EventArgs e)
{
DataTable dtblSelectedCatsInfo = new DataTable();
DataColumn dcolCatNo = new DataColumn("intCatNo");
DataColumn dcolNumOfRoom = new DataColumn("tintNumOfRoom");
CheckBox chkSelectCats;
dtblSelectedCatsInfo.Columns.Add(dcolCatNo);
dtblSelectedCatsInfo.Columns.Add(dcolNumOfRoom);
foreach (GridViewRow gvwRow in gvwCategories.Rows
chkSelectCats = (CheckBox)gvwRow.FindControl("chkSelectRow");
if (chkSelectCats.Checked) {
ListBox lbxRoom=(ListBox)gvwRow.FindControl("lstNumOfRoom");
DataRow drow=dtblSelectedCatsInfo.NewRow();
dtblSelectedCatsInfo.Rows.Add(drow);
drow["intCatNo"]=gvwCategories.DataKeysgvwRow.RowIndex.Value.ToStr ing();
drow["tintNumOfRoom"] = lbxRoom.SelectedItem.Text;
}
}
Session.Add("dtblCatsInfo", dtblSelectedCatsInfo);
{






<asp:GridView ID="gvwCategories" runat="server" Width="100%" CellPadding="3" CellSpacing="1"
GridLines="None" BorderWidth="2px" BorderStyle="Ridge" AutoGenerateColumns="False"
BackColor="Linen" AlternatingRowStyle-BackColor="antiquewhite" DataKeyNames="intRoomCatPerHotelNo"
BorderColor="Tan" Height="176px" >
<HeaderStyle BackColor="Tan" ForeColor="Black" Font-Bold="True" />
<PagerStyle Font-Names="tahoma" Font-Size="11px" HorizontalAlign="Right"/>
<Columns>
<asp:TemplateField HeaderText="نوع دسته">
<ItemTemplate><b><%# Eval("nvcCatType") %></b><%# Eval("comment")%>
</ItemTemplate>
<ItemStyle HorizontalAlign="Justify" Width="40%" /> </asp:TemplateField>
<asp:TemplateField HeaderText="قیمت دسته" >
<ItemTemplate><%# Eval("mnyCatPrice") %></ItemTemplate>
<ItemStyle HorizontalAlign="Center" />]
</asp:TemplateField>
<asp:TemplateField HeaderText="تعداد اتاق" >[/FONT]
<ItemTemplate>
<asp:ListBox runat="server" ID="lstNumOfRoom" Rows="1" >
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
<asp:ListItem>5</asp:ListItem>
<asp:ListItem>6</asp:ListItem>
</asp:ListBox>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField >
<ItemStyle HorizontalAlign="Center" /><ItemTemplate>
<asp:CheckBox ID="chkSelectRow" runat="server"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="btnOk" runat="server" OnClick="SelectCategories" Text="تایید" Font-Names="Tahoma" Font-Size="Small" Height="21px" Width="59px"/></td>



خطایی که رخ می دهد:



Object reference not set to an instance of an object.
Description:An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:

Line 96: dtblSelectedCatsInfo.Rows.Add(drow);
Line 97: drow["intCatNo"]=gvwCategories.DataKeys[gvwRow.RowIndex].Value.ToString();
Line 98: drow["tintNumOfRoom"] = lbxRoom.SelectedItem.Text;

}




باتشکر

anahita_s
چهارشنبه 25 مهر 1386, 14:58 عصر
چرا کسی به من پاسخی نمیدهد؟
خواهشا سریع

amirak
پنج شنبه 26 مهر 1386, 02:02 صبح
سلام .
شما باید برای checkbox یک object از نوع checkbox بسازید و مقدار ستون gridview را به آن پاس دهید .ترجیحاً این عمل را در codebehind انجام دهید .
CheckBox ch = newCheckBox();
int j,cod;
int i = 0;
j = GridView3.Rows.Count;
for (i = 0; i < j; i++)
{
ch = (CheckBox)GridView3.Rows[i].Cells[0].Controls[1];
if (ch.Checked)
{
cod = Convert.ToInt16(GridView3.Rows[i].Cells[2].Text);
در اینجا چک باکس من در اولین ستون از سمت راست گرید قرار دارد . مقدار ستون خاص با استفاده از یک باتوم بدست می آید .

اگر باز متوجه نشده شمارتو بده باهات تماس می گیرم .