datagrid

collie

New member
datagrid

Hi, I am trying to bind my recordset to a datagrid but it doesn't work. I did response.write to see if it in fact receives the correct records and it does. The only problem is with the databinding. Can someone please help me? Thanks aspx.vb Private oConn As New SqlConnection(Constants.SQLProviderConnectionString) Private cmd As SqlCommand Private AD_ApprovedState Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Put user code to initialize the page here fillTable() End Sub Public Sub fillTable() AD_ApprovedState = "1" If AD_ApprovedState = "" Then AD_ApprovedState = "0" End If cmd = oConn.CreateCommand cmd.CommandText = "select * from ads where ad_approved=@AD_ApprovedState" cmd.Parameters.Add("@AD_ApprovedState", AD_ApprovedState) oConn.Open() Dim DR As SqlDataReader DR = cmd.ExecuteReader Do While DR.Read = True ad_title = CStr(DR("ad_title")) ad_poster = CType(DR("ad_poster"), String) ad_man = CType(DR("ad_man"), String) date_expired = CType(DR("ad_expired"), String) date_start = CType(DR("ad_dated"), String) ad_id = CType(DR("ad_id"), Int32) Loop DataGrid1.DataSource = DR DataGrid1.DataBind() DR.Close() oConn.Close() ASPX: <asp:datagrid id="DataGrid1" runat="server"> <Columns> <asp:BoundColumn HeaderText="Title" DataField="ad_title" /> <asp:BoundColumn HeaderText="Owner" DataField="ad_poster" /> <asp:BoundColumn HeaderText="Date Expired" DataField="ad_expired" /> <asp:BoundColumn HeaderText="Date" DataField="ad_dated" /> <asp:BoundColumn HeaderText="Manager" DataField="ad_man" /> <asp:BoundColumn HeaderText="Quantity" DataField="ad_quantity" /> <asp:BoundColumn HeaderText="AD_ID" DataField="ad_id" /> </Columns> </asp:datagrid>​
 

nattygur

New member
הי המעבר על ה dataReader

מיותר, בסופו הגעת לסוף ה DataReader ואין יותר נתונים לעבור עליהם. הקישור ל DataSource של הגריד אמור לעשות את העבודה בשבלך. תוריד את קטע הקוד הזה :
Do While DR.Read = True ad_title = CStr(DR("ad_title")) ad_poster = CType(DR("ad_poster"), String) ad_man = CType(DR("ad_man"), String) date_expired = CType(DR("ad_expired"), String) date_start = CType(DR("ad_dated"), String) ad_id = CType(DR("ad_id"), Int32) Loop​
 

collie

New member
type not defined

Hi, I am trying to add paging to my datagrid but I receive the following error and i don't understand why: Type 'DataGridPageChangedEventArgs' is not defined. Here is my code: aspx.vb Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load fillTable() End Sub Public Sub fillTable() AD_ApprovedState = "1" If AD_ApprovedState = "" Then AD_ApprovedState = "0" End If cmd = oConn.CreateCommand cmd.CommandText = "select *, 'adDetail.aspx?sid=' + @sid + '&ad_id=' + CAST(ad_id AS NVARCHAR(16))+ '&sub_id=' + CAST(sub_id AS NVARCHAR(16))+ '&cat_id=' + CAST(cat_id AS NVARCHAR(16)) AS url_str FROM ybay_mdb...ads where ad_approved=@AD_ApprovedState" cmd.Parameters.Add("@sid", sid) cmd.Parameters.Add("@AD_ApprovedState", AD_ApprovedState) oConn.Open() Dim DR As SqlDataReader DR = cmd.ExecuteReader DataGrid1.DataSource = DR DataGrid1.DataBind() DR.Close() oConn.Close() End Sub Sub doPaging(ByVal s As Object, ByVal e As DataGridPageChangedEventArgs) DataGrid1.CurrentPageIndex = e.NewPageIndex fillTable() End Sub End Class aspx: <asp:datagrid id="DataGrid1" runat="server" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" BackColor="White" CellPadding="3" GridLines="Horizontal" AutoGenerateColumns="false" AllowPaging="True" PageSize="5" PagerStyle-HorizontalAlign="Center" OnPageIndexChanged="doPaging"> .......... </datagrid>​
 

gooshi

New member
Paging a DataGrid

Hi, First of all you should use DataSet(DataTable really) instead of SqlDataReader, beacuse by default it does not support paging. try this:
Dim da As New SqlDataAdapter Dim ds As New DataSet .... oConn.Open() cmd = New SqlCommand("select * from loanerProperties", oConn) da.SelectCommand = cmd da.Fill(ds) DataGrid1.DataSource = ds.Tables(0) DataGrid1.DataBind() ds.Dispose() oConn.Close() ..... Sub doPaging(ByVal s As Object, ByVal e As DataGridPageChangedEventArgs) DataGrid1.CurrentPageIndex = e.NewPageIndex fillTable() End Sub​
I found this in another forum, I quote: "The use of a DataReader requires a CustomPaging solution. Since the DataReader returns a stream, the .NET Framework does not do the automatic page calculations that it does when using a DataTable or DataView, where all of the data is in memory at once. You have to manage retreiving the correct records to display (custom paging will display the first record through the number assigned to the PageSize property). The VirtualItem count is the total number of records. The .NET Framework uses this to calculate how many pages to render links for (VirtualItemCount/PageSize). Basically, you need to: - Retreive only the records you are showing in the current page - Set VirtualItemCount to the total number of records - Set PageSize to the number of records that you are rendering per page - Set AllowCustomPaging=True and AllowPaging=True" The Goosh
 

collie

New member
datagrid dir=rtl

Hi, The contents of my datagrid are in Hebrew. How can I do dir=rtl? Do i need to add div? Thanks​
 

gooshi

New member
direction

Are u sure you want to do that? diraction=rtl will reverse the order of all your TD elements. you should add that on the itemTemplate/alternate element or in the itemDataBound event inorder to set direction only on TD or TR but not on the TABLE element. The Goosh
 

gooshi

New member
למה זה עדיף?

קודם כל, זה בטח ענין של הרגל, אם אתה רגיל לכתוב טבלאות ב HTML עם DIR=RTL , תמשיך ככה. אני אישית חושב שזה לא טבעי לכתוב קודם מה שבא מימין ואח"כ מה שיבוא משמאל מבחינת תחזוקת הקוד. אני מניח (זה לא משהו מדעי) שרוב מי שקורא HTML מניח שמה שבא קודם מוצג קודם ומבחינת הדפדפן זה אומר משמאל לימין. זה נכון לסדר של אובייקטים בשורה וזה נכון לסדר של תאים בטבלה. הדפדפן יודע שהוא מצייר את המידע משמאל לימין. אמת, אם בסופו של דבר כל הדברים שהוא יצייר הם מילים בעברית, אזי הוא ידע להציג אותם מימין לשמאל וזה בגלל העברית הלוגית שמחייבת את הדפדפן להפוך את הסדר. לסיכום, מי שרגיל יכול לעבוד עם DIR=RTL על טבלה או GRID אבל זה בטח זה אינטואיטיבי למפתחי WEB אחרים לתמוך בזה. זו דעתי...
 

Admini

New member
אני כבר שנים עובד עם dir=rtl

פשוט לעבוד משמאל לימין נשמע לי מעוות...
 

collie

New member
datagrid and image

Hi, I have a table in Access that contains an image field. However, not the full path is given in the db only the name of the image and extension such as pic1.jpg. The full path is somethink like this site/images/ pic1.jpg Can someone please tell me how to add an image to the datagrid? Thanks in advance. If Not Page.IsPostBack Then fillTable() End If End Sub Public Sub fillTable() AD_ApprovedState = "1" If AD_ApprovedState = "" Then AD_ApprovedState = "0" End If cmd = oConn.CreateCommand cmd.CommandText = "select *, 'adDetail.aspx?sid=' + @sid + '&ad_id=' + CAST(ad_id AS NVARCHAR(16))+ '&sub_id=' + CAST(sub_id AS NVARCHAR(16))+ '&cat_id=' + CAST(cat_id AS NVARCHAR(16)) AS url_str FROM ybay_mdb...ads where ad_approved=@AD_ApprovedState" cmd.Parameters.Add("@sid", sid) cmd.Parameters.Add("@AD_ApprovedState", AD_ApprovedState) Dim da As New SqlDataAdapter(cmd) Dim dt As New DataTable("ads") da.Fill(dt) DataGrid1.DataSource = dt DataGrid1.DataBind() End Sub Sub doPaging(ByVal s As Object, ByVal e As DataGridPageChangedEventArgs) Handles DataGrid1.PageIndexChanged DataGrid1.CurrentPageIndex = e.NewPageIndex fillTable() End Sub​
 

gooshi

New member
try this

When binding the grid for each row in your data source you get the Container.DataItem object, that is the DataRowView or ant row of data givven to the grid as the data source. in case the source is a dataView, you get the "imagesPath" value this way: Container.DataItem("imagePath") in case the source is an Array of objects you get it this way: Container.DataItem.imagePath meaning the "imagePath" is a property of that object.
<asp:DataGrid id="DataGrid1" runat="server"> <Columns> <asp:TemplateColumn> <ItemTemplate> <Img src="/site/images/<%# Container.DataItem("imagePath") %>" id="img" runat="server" /> </ItemTemplate> </asp:TemplateColumn> </Columns> </asp:DataGrid>​
jops that helps.
 

yuval k

New member
gooshi - אתה יכול בבקשה ליישר לשמאל

כשאתה עונה באנגלית?
זה כואב לקרוא אנגלית ככה
 

gooshi

New member
האמת...

זה רעיון לא רע ליישר לשמאל
ככה כולם יבינו! לא יהיו בעיות ושלום על ישראל!!!​
 
למעלה