Updateable Data Grid using Linq to SQL in WinForms - vb.net

I'm populating a datagrid using Linq--standard kind of stuff (code below). For some reason however my ultraDataGrid is stuck in some kind of read-only mode. I've checked all the grid properties I can think of. Is that a result of binding to a Linq data source? Anyone have example code of an updatable grid that uses Linq?
db = New DataContext
myData = New dataClass
dataUltraGrid.DataSource = From table _
In db.profiles _
Select table.field1, table.field2...

Your really not using a LinqDataSource control... your are binding to a List db.profiles your data grid doesn't know anything about updating or deleting or inserting by just being bound to that list, can I suggets this:
<asp:GridView ID="GridView1" runat="server" DataSourceID="LinqDataSource1"
AutoGenerateColumns="False" DataKeyNames="FooID">
<Columns>
<asp:BoundField DataField="FooID" HeaderText="FooID" InsertVisible="False"
ReadOnly="True" SortExpression="FooID" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Description" HeaderText="Description"
SortExpression="Description" />
</Columns>
</asp:GridView>
<asp:LinqDataSource ID="LinqDataSource1" runat="server"
ContextTypeName="YourDataContext" EnableDelete="True"
EnableInsert="True" EnableUpdate="True" TableName="Foos">
</asp:LinqDataSource>

Found the solution: use lambda expressions to filter the entity and bind directly to the entity.

Related

Gridview Calculated Field in Row

I am using Visual Studio 2010 and Visual Basic linked to a SQL database. I have a Gridview in which I want to have a calculated field, let's call it NoHoursOff, based on the fields in that row - BeginTimeOff and EndTimeOff.
After doing some research, I learned that I should use a TemplateField. I just don't understand how this works. I have tried with no success:
<asp:TemplateField HeaderText="Total Hours" >
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# "EndTimeOff" - "BeginTimeOff" %>' Width="100px"></asp:Label>>
</ItemTemplate>
</asp:TemplateField>
I also have tried replacing Text value with "EndTimeOff".subtract("BeginTimeOff") which works on another webpage. I don't understand how to do a calculated field! Please help or can you direct me to a tutorial that would explain this to me.
You need to create a function in the codebehind like this
public string getValue(object BeginTimeOff, object EndTimeOff)
{
return (Convert.ToDateTime(EndTimeOff.ToString()) - Convert.ToDateTime(BeginTimeOff.ToString())).ToString();
}
and in the Label this
Text=<%# getValue(Eval("BeginTimeOff"), Eval("EndTimeOff")) %>
then just set the format you want to display in the method (n days, hh.mm.ss, etc) to fit your requirement
Per your suggestions, I can't get it to work. Here is my aspx and vb code:
<asp:TemplateField HeaderText="TotalHours2" >
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# getValue(Eval("BeginTimeOff"), Eval("EndTimeOff")) %>' Width="100px"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
AND
Public Function getValue(BeginTimeOff As Object, EndTimeOff As Object) As String
Return (DateTime.Parse(EndTimeOff.ToString()) - DateTime.Parse(BeginTimeOff.ToString())).ToString()
End Function
It's probably something wrong with my attempt to convert C# to VB. Thanks for all your help! (FYI - I did get another function to work using your example - just not this new function. If you want to see code, I'll show it to you. It counts days between 2 dates, not including weekends and holidays. Thanks a million for helping me figure that one out!)

How to get gridview clickable cell data value to new page aspx

I have two columns in a GridView where every cell of 1 column is hyperlinked.
<asp:GridView ID="gvBlogList" runat="server" DataKeyNames="Id"
AutoGenerateColumns="False" OnRowDataBound="OnRowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLinkField DataTextField="id"
HeaderText="Blog id" NavigateUrl="detailspage.aspx" />
</ItemTemplate>
<ItemStyle Width="150px"></ItemStyle>
</asp:TemplateField>
</Columns>
</asp:GridView>
How can I get cell value in 2nd page as a session variable?
Thanks!
As per the what I understand from your question, you want to catch the value of the cell that is clicked, store it in session and make it accessible on the page that have redirect to. This is possible and you can try a flow like this :
You can use the GridView's inbuilt method onRowCommand in this case to generate a server side event and you can then identify which cell was clicked and lastly, you can get its value by index. The below code is a demonstration that you can try and manipulate as per your requirement. This is a sample where a Linkbutton is used in Gridview and on the click of it, its value is fetched.
You can also try to use the CommandName and CommandArgument properties if you want to assign that particular control a value that you may use at the server side.
protected void Grd1_RowCommand(object sender, GridViewCommandEventArgs e)
{
LinkButton lnk = (LinkButton)e.CommandSource;
string value = lnk.Text;
}
And for storing it in Session, you just have to make use of Session["yourVarName"] = value;. And this value can be accessed on the other page.
Hope this helps.

How to pass the querystring parameter into FormControl in vb.net

I am passing a value from one form to the other. The second form in inserting the data field into the table. How do I pass the ID(strPID) passed from the first form into the form control PatronIDTextBox of the second form?
VB.Code: on Page load -- strPID = Request.QueryString("value1")
Form control:
<InsertItemTemplate>
PatronID:
<asp:TextBox ID="PatronIDTextBox" runat="server" Text='<%# Bind("PatronID") %>' />
Thanks
Ok, Now I am trying to say it this way in object datasource so I can send it into insert statement without being displayed on the screen.
<Insert Parameters>
<asp:QueryStringParameter Name="PatronID" Type="int32" QueryStringField="value1" />
<asp:QueryStringParameter Name="PatronName" Type="String" QueryStringField="value2" />
</InsertParameter>
InsertCommand="INSERT INTO [tblIncident] ([PatronID], [PatronName]) values (#PatronID, #PatronName)
Have you tried directly putting the value into the textbox? Like this:
<asp:TextBox ID="PatronIDTextBox" runat="server" Text='<%= Request.QueryString("Value1") %>' />
Just be careful, because this approach could allow a XSS attack. You might want to read-up on those.
I deleted the text box for both id and name and instead I modified the objectdatasource Insert statement as follows. The value of value1 and value2 is captured on page load event.
<InsertParameters>
<asp:QueryStringParameter Name="PatronID" Type="Int32" QueryStringField="value1" />
<asp:QueryStringParameter Name="PatronName" Type="String" QueryStringField="value2"/>
</InsertParameters>

visual studio 2010 Getting value from gridview

How do I get the value in a gridview when one hits the "Select" link in a row?
If I have a databound, I can get that value. However I am trying to highlight certain labels.The "HighlightTex" function lets me highlight some words that the user selects.
Here is a snippet: I am trying to get the value of "lblinvnum".
<asp:GridView ID="gvDetails" runat="server" AutoGenerateColumns="False" AllowPaging="True"
AllowSorting="True" DataSourceID="SqlDataSource1" Width="540px"
CssClass="Gridview" >
<HeaderStyle BackColor="#df5015" />
<Columns>
<asp:TemplateField HeaderText="Invnum" ItemStyle-Wrap="False">
<ItemTemplate>
<asp:Label ID="lblInvnum" Text='<%# HighlightText(Eval("Invnum").ToString()) %>' runat="server" />
</ItemTemplate>
...
...
... For the "AmtPaid" listed below, I can Get that value. I cannot figure out how to get the value for "lblInvnum" to the snippet listed above.
<asp:BoundField DataField="AmtPaid" HeaderText="AmtPaid"
SortExpression="AmtPaid" DataFormatString="{0:c}" >
In visual basic, this is how I can get the one above for AmtPaid.
Dim index As Integer = Convert.ToInt32(e.CommandArgument)
Dim selectedRow As GridViewRow = gvDetails.Rows(index)
Dim contactCell As TableCell = selectedRow.Cells(1)
Dim contact As String = contactCell.Text
Suggestions?

Add static and dynamic values to ComboBox in vb.net

I want to add 'All' option on top of other records fetched from database shown in dropdownlist.Any ideas on how to do this in vb.net 2005?
Think around the problem. Instead of adding the item to the combobox, add the item to the dynamic datasource.
Even in VS 2005 the AppendDataBoundItems property was available. So you can add this item programmatically from codebehind or declaratively on aspx:
<asp:DropDownList
runat="server"
ID="DropDownList1"
AppendDataBoundItems="true"
>
<asp:ListItem
Enabled="True"
Selected="True"
Text="All"
Value="0"
/>
</asp:DropDownList>
Now all your databound items are appended automatically and the first item will not be removed.
below worked for me!
Add a new row to datasource
Dim dr As DataRow = ds.Tables("cusPracticeLocation").NewRow
'Add some data to it
dr(0) = 0
dr(1) = "All"
ds.Tables("cusPracticeLocation").Rows.InsertAt(dr, 0)