dash420
asked on
Dropdrown in datagrid
I want to put dropdrown in the datagrid to update. as drop drow is not defined always. it will appears onclick of edit command. my problem is that i am unable to set previous value in combo box to selected. only first value getting selected.
my code is like this.
<asp:DataGrid id="dgBookList" Width="600" Runat="server" AllowCustomPaging="False" AutoGenerateColumns="False " PagerStyle-Visible="true" HeaderStyle-CssClass="GRID TH" AlternatingItemStyle-BackC olor="ligh tgrey" BackColor="white" CellPadding="2" CellSpacing="2" DataKeyField="book_number" PagerStyle-Position="TopAn dBottom" PagerStyle-Mode="NumericPa ges" PagerStyle-HorizontalAlign ="Right" OnPageIndexChanged="DG_pag ed" OnEditCommand="doItemEdit" OnUpdateCommand="doItemUpd ate" OnCancelCommand="doItemCan cel" GridLines="None">
<asp:TemplateColumn HeaderText="Author Name">
<columns>
...
... other control
<ItemTemplate>
<asp:Label Text='<%#Container.DataIte m("user_id ")%>' Runat="server" ID="lbluserId">
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList Runat="server" Id="selUser"
DataSource='<%# GetUser(DataBinder.Eval(Co ntainer.Da taItem, "user_id")) %>'
DataValueField="user_id"/>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn EditText="Edit" CancelText="Cancel" UpdateText="Update" />
</Columns>
</asp:DataGrid>
in codebehind
Function GetUser(ByVal struserId As String)
Dim strSql As String
strSql = "SELECT user_id ,user_name FROM users"
Dim objDa As New SqlDataAdapter(strSql, objConnection)
Dim objDs As New DataSet()
objDa.Fill(objDs, "users")
'If blnflag Then
selUser.SelectedItem.Value = lbluserId.Text
'End If
Return objDs.Tables("users")
End Function
also if column value is null, it giving error "from type 'DBNull' to type 'String' is not valid."
cna any suggests how to do it.
my code is like this.
<asp:DataGrid id="dgBookList" Width="600" Runat="server" AllowCustomPaging="False" AutoGenerateColumns="False
<asp:TemplateColumn HeaderText="Author Name">
<columns>
...
... other control
<ItemTemplate>
<asp:Label Text='<%#Container.DataIte
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList Runat="server" Id="selUser"
DataSource='<%# GetUser(DataBinder.Eval(Co
DataValueField="user_id"/>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn EditText="Edit" CancelText="Cancel" UpdateText="Update" />
</Columns>
</asp:DataGrid>
in codebehind
Function GetUser(ByVal struserId As String)
Dim strSql As String
strSql = "SELECT user_id ,user_name FROM users"
Dim objDa As New SqlDataAdapter(strSql, objConnection)
Dim objDs As New DataSet()
objDa.Fill(objDs, "users")
'If blnflag Then
selUser.SelectedItem.Value
'End If
Return objDs.Tables("users")
End Function
also if column value is null, it giving error "from type 'DBNull' to type 'String' is not valid."
cna any suggests how to do it.
You've got the right idea here, but you're trying to set the selected index in the wrong place.
<EditItemTemplate>
<asp:DropDownList Runat="server" Id="selUser"
DataSource='<%# GetUser(DataBinder.Eval(Co ntainer.Da taItem, "user_id")) %>'
DataValueField="user_id"/>
</EditItemTemplate>
You need to add another property here,
SelectedIndex="
<%# GetSelIndex(Container.Data Item("Fiel dId")) %>"
Inside your GetSelIndex function (which should return an int), loop through your dataset until you find the current value...
(sorry, this is C#, I don't use VB)
public int GetSelIndex(string FieldId)
{
DataTable tbl = dataset.Tables["YourTable" ];
for (int i = 0; i < tbl.Rows.Count; i++)
{
if (tbl.Rows[i]["FieldId"].To String() == FieldId)
return i;
}
}
For more info, see question
https://www.experts-exchange.com/questions/20509280/DropDownList-within-a-DataGrid.html
<EditItemTemplate>
<asp:DropDownList Runat="server" Id="selUser"
DataSource='<%# GetUser(DataBinder.Eval(Co
DataValueField="user_id"/>
</EditItemTemplate>
You need to add another property here,
SelectedIndex="
<%# GetSelIndex(Container.Data
Inside your GetSelIndex function (which should return an int), loop through your dataset until you find the current value...
(sorry, this is C#, I don't use VB)
public int GetSelIndex(string FieldId)
{
DataTable tbl = dataset.Tables["YourTable"
for (int i = 0; i < tbl.Rows.Count; i++)
{
if (tbl.Rows[i]["FieldId"].To
return i;
}
}
For more info, see question
https://www.experts-exchange.com/questions/20509280/DropDownList-within-a-DataGrid.html
Forgot to add....
Get rid of the selUser.SelectedItem code in the GetUser function, you don't need it.
Function GetUser(ByVal struserId As String)
Dim strSql As String
strSql = "SELECT user_id ,user_name FROM users"
Dim objDa As New SqlDataAdapter(strSql, objConnection)
Dim objDs As New DataSet()
objDa.Fill(objDs, "users")
Return objDs.Tables("users")
End Function
Get rid of the selUser.SelectedItem code in the GetUser function, you don't need it.
Function GetUser(ByVal struserId As String)
Dim strSql As String
strSql = "SELECT user_id ,user_name FROM users"
Dim objDa As New SqlDataAdapter(strSql, objConnection)
Dim objDs As New DataSet()
objDa.Fill(objDs, "users")
Return objDs.Tables("users")
End Function
This works for me...
Declare a Global Variable, Like This:
#Region "Public Variable Declarations"
Public gCurrentType As String
#End Region
Within the Edit Event, capture the value of the current record and store it to the Global variable. In this example, I assume the control is named DropDownList1. Like this:
Public Sub MyDataGrid_Edit(ByVal Sender As Object, ByVal e As DataGridCommandEventArgs)
...
' Capture Current Values for Item to Be Editted
gCurrentType = CType(e.Item.FindControl(" DropDownLi st1"), Label).Text
...
End Sub
Then code for the ItemDataBound event to assign the control's SelectedIndex property. Since I am using the Name value of the control instead of the Value property, I use the FindByText method to get the Index.
Public Sub MyDataGrid_ItemDataBound(B yVal s As Object, ByVal e As DataGridItemEventArgs)
' Presets the DropDownLists to the Current Value
If e.Item.ItemType = ListItemType.EditItem Then
' Item Types
Dim myTypeDropDown As DropDownList
myTypeDropDown =
CType(e.Item.FindControl(" DropDownLi st1"), DropDownList)
myTypeDropDown.SelectedInd ex =
myTypeDropDown.Items.Index Of(myTypeD ropDown.It ems.FindBy Text(gCurr entType))
End If
End Sub
Like I said, this works perfectly for me. If you still have some problems, post again.
Declare a Global Variable, Like This:
#Region "Public Variable Declarations"
Public gCurrentType As String
#End Region
Within the Edit Event, capture the value of the current record and store it to the Global variable. In this example, I assume the control is named DropDownList1. Like this:
Public Sub MyDataGrid_Edit(ByVal Sender As Object, ByVal e As DataGridCommandEventArgs)
...
' Capture Current Values for Item to Be Editted
gCurrentType = CType(e.Item.FindControl("
...
End Sub
Then code for the ItemDataBound event to assign the control's SelectedIndex property. Since I am using the Name value of the control instead of the Value property, I use the FindByText method to get the Index.
Public Sub MyDataGrid_ItemDataBound(B
' Presets the DropDownLists to the Current Value
If e.Item.ItemType = ListItemType.EditItem Then
' Item Types
Dim myTypeDropDown As DropDownList
myTypeDropDown =
CType(e.Item.FindControl("
myTypeDropDown.SelectedInd
myTypeDropDown.Items.Index
End If
End Sub
Like I said, this works perfectly for me. If you still have some problems, post again.
ASKER
hi Frostbyte_Zero
this line gives me problem
' Capture Current Values for Item to Be Editted
gCurrentType = CType(e.Item.FindControl(" DropDownLi st1"), Label).Text
errir is like this
"
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.NullReferenceExcept ion: Object reference not set to an instance of an object.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
"
this line gives me problem
' Capture Current Values for Item to Be Editted
gCurrentType = CType(e.Item.FindControl("
errir is like this
"
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.NullReferenceExcept
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
"
ASKER
hi wiseguy_2112
i tried your method it is also fails. givng error something like uanble to declare runtime property
i tried your method it is also fails. givng error something like uanble to declare runtime property
post your code (just the itemtemplate and you code behind) and we'll try to figure out what the problem is.
ASKER
Hope u can find out the problem
Imports System.Data
Imports System.Data.SqlClient
Public Class editdatagrid
Inherits System.Web.UI.Page
Protected WithEvents dgBookList As System.Web.UI.WebControls. DataGrid
Protected WithEvents selUser As DropDownList
Protected WithEvents lbluserId As Label
Protected btnEditColumn As System.Web.UI.WebControls. EditComman dColumn
Protected blnflag As Boolean = False
Protected objUtility = New utility()
Protected WithEvents Table1 As System.Web.UI.WebControls. Table
Dim objConnection As New SqlConnection(Configuratio nSettings. AppSetting s("connStr ing"))
#Region " Web Form Designer Generated Code "
'Public seluserid As String
'This call is required by the Web Form Designer.
<System.Diagnostics.Debugg erStepThro ugh()> Private Sub InitializeComponent()
End Sub
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
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
If Not IsPostBack Then
bindGrid()
'btnEditColumn. .Attributes.Add("onclick", "return fnTest();")
End If
End Sub
Sub bindGrid()
Dim objDA As SqlDataAdapter
Dim objDS As DataSet
Dim strSql As String
Dim intPageNo As Integer
Dim intPageSize As Integer
Dim intRecCount As Integer
strSql = " SELECT book_number,title_name,aut hor_name,p ublisher,n o_of_copie s,user_id FROM book " & "_"
strSql = strSql & " ORDER BY book_number"
objDA = New SqlDataAdapter(strSql, objConnection)
objDS = New DataSet()
intPageNo = CInt(dgBookList.CurrentPag eIndex) + 1
If blnflag Then
Response.Write("dkjdj")
'selUser.SelectedIndex = lbluserId.Text
End If
Try
objDA.Fill(objDS, "book")
intRecCount = objDS.Tables("book").Rows. Count.ToSt ring()
dgBookList.PageSize = 10
intPageSize = CInt(dgBookList.PageSize)
If intRecCount > 2 Then
dgBookList.AllowPaging = True
End If
dgBookList.DataSource = objDS.Tables("book").Defau ltView
dgBookList.DataBind()
Catch e As Exception
dgBookList.CurrentPageInde x = 0
Response.Write(Err.Descrip tion)
End Try
End Sub
Sub DG_paged(ByVal Sender As Object, ByVal E As DataGridPageChangedEventAr gs)
dgBookList.CurrentPageInde x = E.NewPageIndex
bindGrid()
End Sub
Sub doItemEdit(ByVal objSource As Object, ByVal objArgs As DataGridCommandEventArgs)
dgBookList.EditItemIndex = objArgs.Item.ItemIndex
blnflag = True
bindGrid()
'Dim objUserTo = CType(objArgs.Item.FindCon trol("selU ser"), DropDownList)
'seluserid = CType(objArgs.Item.FindCon trol("selU ser"), Label).Text
'Dim objDataGrid = CType(objSource, DataGrid)
'objDataGrid.EditItemIndex = objArgs.Item.ItemIndex
' selUser.SelectedIndex = "3" 'selUser.Items.IndexOf(sel User.Items .FindByTex t("george" ))
End Sub
Sub doItemCancel(ByVal objSource As Object, ByVal objArgs As DataGridCommandEventArgs)
dgBookList.EditItemIndex = -1
'populateUser()
bindGrid()
End Sub
Function GetUser()
Dim strSql As String
strSql = "SELECT 1 as mysortval, 'Select' as user_id,'' as user_name FROM users UNION "
strSql = strSql & "SELECT 2 as mysortval, user_id ,user_name FROM users"
Dim objDa As New SqlDataAdapter(strSql, objConnection)
Dim objDs As New DataSet()
objDa.Fill(objDs, "users")
'If blnflag Then
'selUser.SelectedItem.Valu e = lbluserId.Text
'End If
Return objDs.Tables("users")
End Function
Sub doItemUpdate(ByVal objSource As Object, ByVal objArgs As DataGridCommandEventArgs)
Dim objBookTitle = CType(objArgs.Item.FindCon trol("txtT itle"), TextBox)
Dim objAuthor = CType(objArgs.Item.FindCon trol("txtA uthor"), TextBox)
Dim objPublisher = CType(objArgs.Item.FindCon trol("txtP ublisher") , TextBox)
Dim objCopies = objArgs.Item.Cells(4).Cont rols(0)
'Dim objIssueTo = objArgs.Item.Cells(5).Cont rols(0)
Dim objIssueTo = CType(objArgs.Item.FindCon trol("selU ser"), DropDownList)
Dim strSql As String
strSql = "UPDATE book set title_name = " & objUtility.QuotedString(ob jBookTitle .Text) & "," & _
"author_name = " & objUtility.QuotedString(ob jAuthor.Te xt) & "," & _
"publisher = " & objUtility.QuotedString(ob jPublisher .Text) & "," & _
"no_of_copies = " & objCopies.Text & ","
If objIssueTo.SelectedItem.Te xt <> "Select" Then
strSql = strSql & "user_id = " & objUtility.QuotedString(ob jIssueTo.S electedIte m.Text) & ""
Else
strSql = strSql & "user_id = NULL"
End If
strSql = strSql & " WHERE book_number = " & dgBookList.DataKeys(objArg s.Item.Ite mIndex)
'Response.Write(strSql)
Dim objCommand As New SqlCommand(strSql, objConnection)
Try
objConnection.Open()
objCommand.ExecuteNonQuery ()
objConnection.Close()
Catch Exc As SqlException
Response.Write("When executing Query" & Exc.Message & "Query : " & strSql)
Response.End()
End Try
dgBookList.EditItemIndex = -1
'populateUser()
bindGrid()
End Sub
'Public Sub dgBookList_ItemDataBound(B yVal s As Object, ByVal e As DataGridItemEventArgs)
' ' Presets the DropDownLists to the Current Value
' If e.Item.ItemType = ListItemType.EditItem Then
' ' Item Types
' Dim myTypeDropDown As DropDownList
' myTypeDropDown = CType(e.Item.FindControl(" seluserid" ), DropDownList)
' myTypeDropDown.SelectedInd ex = myTypeDropDown.Items.Index Of(myTypeD ropDown.It ems.FindBy Text(selus erid))
' End If
'End Sub
End Class
Imports System.Data
Imports System.Data.SqlClient
Public Class editdatagrid
Inherits System.Web.UI.Page
Protected WithEvents dgBookList As System.Web.UI.WebControls.
Protected WithEvents selUser As DropDownList
Protected WithEvents lbluserId As Label
Protected btnEditColumn As System.Web.UI.WebControls.
Protected blnflag As Boolean = False
Protected objUtility = New utility()
Protected WithEvents Table1 As System.Web.UI.WebControls.
Dim objConnection As New SqlConnection(Configuratio
#Region " Web Form Designer Generated Code "
'Public seluserid As String
'This call is required by the Web Form Designer.
<System.Diagnostics.Debugg
End Sub
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
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
If Not IsPostBack Then
bindGrid()
'btnEditColumn. .Attributes.Add("onclick",
End If
End Sub
Sub bindGrid()
Dim objDA As SqlDataAdapter
Dim objDS As DataSet
Dim strSql As String
Dim intPageNo As Integer
Dim intPageSize As Integer
Dim intRecCount As Integer
strSql = " SELECT book_number,title_name,aut
strSql = strSql & " ORDER BY book_number"
objDA = New SqlDataAdapter(strSql, objConnection)
objDS = New DataSet()
intPageNo = CInt(dgBookList.CurrentPag
If blnflag Then
Response.Write("dkjdj")
'selUser.SelectedIndex = lbluserId.Text
End If
Try
objDA.Fill(objDS, "book")
intRecCount = objDS.Tables("book").Rows.
dgBookList.PageSize = 10
intPageSize = CInt(dgBookList.PageSize)
If intRecCount > 2 Then
dgBookList.AllowPaging = True
End If
dgBookList.DataSource = objDS.Tables("book").Defau
dgBookList.DataBind()
Catch e As Exception
dgBookList.CurrentPageInde
Response.Write(Err.Descrip
End Try
End Sub
Sub DG_paged(ByVal Sender As Object, ByVal E As DataGridPageChangedEventAr
dgBookList.CurrentPageInde
bindGrid()
End Sub
Sub doItemEdit(ByVal objSource As Object, ByVal objArgs As DataGridCommandEventArgs)
dgBookList.EditItemIndex = objArgs.Item.ItemIndex
blnflag = True
bindGrid()
'Dim objUserTo = CType(objArgs.Item.FindCon
'seluserid = CType(objArgs.Item.FindCon
'Dim objDataGrid = CType(objSource, DataGrid)
'objDataGrid.EditItemIndex
' selUser.SelectedIndex = "3" 'selUser.Items.IndexOf(sel
End Sub
Sub doItemCancel(ByVal objSource As Object, ByVal objArgs As DataGridCommandEventArgs)
dgBookList.EditItemIndex = -1
'populateUser()
bindGrid()
End Sub
Function GetUser()
Dim strSql As String
strSql = "SELECT 1 as mysortval, 'Select' as user_id,'' as user_name FROM users UNION "
strSql = strSql & "SELECT 2 as mysortval, user_id ,user_name FROM users"
Dim objDa As New SqlDataAdapter(strSql, objConnection)
Dim objDs As New DataSet()
objDa.Fill(objDs, "users")
'If blnflag Then
'selUser.SelectedItem.Valu
'End If
Return objDs.Tables("users")
End Function
Sub doItemUpdate(ByVal objSource As Object, ByVal objArgs As DataGridCommandEventArgs)
Dim objBookTitle = CType(objArgs.Item.FindCon
Dim objAuthor = CType(objArgs.Item.FindCon
Dim objPublisher = CType(objArgs.Item.FindCon
Dim objCopies = objArgs.Item.Cells(4).Cont
'Dim objIssueTo = objArgs.Item.Cells(5).Cont
Dim objIssueTo = CType(objArgs.Item.FindCon
Dim strSql As String
strSql = "UPDATE book set title_name = " & objUtility.QuotedString(ob
"author_name = " & objUtility.QuotedString(ob
"publisher = " & objUtility.QuotedString(ob
"no_of_copies = " & objCopies.Text & ","
If objIssueTo.SelectedItem.Te
strSql = strSql & "user_id = " & objUtility.QuotedString(ob
Else
strSql = strSql & "user_id = NULL"
End If
strSql = strSql & " WHERE book_number = " & dgBookList.DataKeys(objArg
'Response.Write(strSql)
Dim objCommand As New SqlCommand(strSql, objConnection)
Try
objConnection.Open()
objCommand.ExecuteNonQuery
objConnection.Close()
Catch Exc As SqlException
Response.Write("When executing Query" & Exc.Message & "Query : " & strSql)
Response.End()
End Try
dgBookList.EditItemIndex = -1
'populateUser()
bindGrid()
End Sub
'Public Sub dgBookList_ItemDataBound(B
' ' Presets the DropDownLists to the Current Value
' If e.Item.ItemType = ListItemType.EditItem Then
' ' Item Types
' Dim myTypeDropDown As DropDownList
' myTypeDropDown = CType(e.Item.FindControl("
' myTypeDropDown.SelectedInd
' End If
'End Sub
End Class
I need to see your aspx code for the grid also. What, specifically is the error and where in the code is the error occurring? I don't see a function to get the currently selected item.
ASKER
this my datagrid control, as already told there is no SelectedIndex method here. If i put SelectedIndex in datagrid it is giving error like "unable to declare runtime properties". where to invoke SelectedIndex method, hope you can make out what is my problem.
<asp:DataGrid id="dgBookList" Width="600" Runat="server" AllowCustomPaging="False" AutoGenerateColumns="False " PagerStyle-Visible="true" HeaderStyle-CssClass="GRID TH" AlternatingItemStyle-BackC olor="ligh tgrey" BackColor="white" CellPadding="2" CellSpacing="2" DataKeyField="book_number" PagerStyle-Position="TopAn dBottom" PagerStyle-Mode="NumericPa ges" PagerStyle-HorizontalAlign ="Right" OnPageIndexChanged="DG_pag ed" OnEditCommand="doItemEdit" OnUpdateCommand="doItemUpd ate" OnCancelCommand="doItemCan cel" GridLines="None">
<Columns>
<asp:BoundColumn DataField="book_number" HeaderText="Book No" ReadOnly="True"></asp:Boun dColumn>
<asp:TemplateColumn HeaderText="Book Title">
<ItemTemplate>
<asp:Label Text='<%#Container.DataIte m("title_n ame")%>' Runat="server" ID="Label1">
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID ="txtTitle" Text='<%#Container.DataIte m("title_n ame")%>' Runat="server"/>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Author Name">
<ItemTemplate>
<asp:Label Text='<%#Container.DataIte m("author_ name")%>' Runat="server" ID="Label2">
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID ="txtAuthor" Text='<%#Container.DataIte m("author_ name")%>' Runat="server"/>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Author Name">
<ItemTemplate>
<asp:Label Text='<%#Container.DataIte m("Publish er")%>' Runat="server" ID="Label3">
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID ="txtPublisher" Text='<%#Container.DataIte m("Publish er")%>' Runat="server"/>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="no_of_copies" HeaderText="Copies"></asp: BoundColum n>
<asp:TemplateColumn HeaderText="Author Name">
<ItemTemplate>
<asp:Label Text='<%#Container.DataIte m("user_id ")%>' Runat="server" ID="lbluserId">
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList Runat="server" Id="selUser"
DataSource='<%#GetUser()%> '
DataValueField="user_id" />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn EditText="Edit" CancelText="Cancel" UpdateText="Update" />
</Columns>
</asp:DataGrid>
<asp:DataGrid id="dgBookList" Width="600" Runat="server" AllowCustomPaging="False" AutoGenerateColumns="False
<Columns>
<asp:BoundColumn DataField="book_number" HeaderText="Book No" ReadOnly="True"></asp:Boun
<asp:TemplateColumn HeaderText="Book Title">
<ItemTemplate>
<asp:Label Text='<%#Container.DataIte
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID ="txtTitle" Text='<%#Container.DataIte
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Author Name">
<ItemTemplate>
<asp:Label Text='<%#Container.DataIte
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID ="txtAuthor" Text='<%#Container.DataIte
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Author Name">
<ItemTemplate>
<asp:Label Text='<%#Container.DataIte
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID ="txtPublisher" Text='<%#Container.DataIte
</EditItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="no_of_copies" HeaderText="Copies"></asp:
<asp:TemplateColumn HeaderText="Author Name">
<ItemTemplate>
<asp:Label Text='<%#Container.DataIte
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList Runat="server" Id="selUser"
DataSource='<%#GetUser()%>
DataValueField="user_id" />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn EditText="Edit" CancelText="Cancel" UpdateText="Update" />
</Columns>
</asp:DataGrid>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi
="<%# GetSelIndex(Container.Data Item("user id")) %>" is failing if it is null. how to avoid it.
error is like this
Cast from type 'DBNull' to type 'String' is not valid.
as GetSelIndex function signature is like
Function GetSelIndex(ByVal FieldId As String)
="<%# GetSelIndex(Container.Data
error is like this
Cast from type 'DBNull' to type 'String' is not valid.
as GetSelIndex function signature is like
Function GetSelIndex(ByVal FieldId As String)
ASKER
hi gorgot to write this now able to select the value in drop down. only if it null it is giving error.
ASKER
Don't mind, How to validate in the datagrid. like suppose user enter varchar in the integer field.
ASKER
i able to solve null problem. Do u know how to validate data grid while editing. like user enter varchar where fields is number.
Thanks for the help.
regards,
Dash
Thanks for the help.
regards,
Dash
Sorry Guys ... Just got back to this Forum.
Looks like you guys figred it out. Unfortunately, there is no way to use validation controls when you use in-place editing on a datagrid.
The validation controls seem to be tied to a control on the form. Since techincally not all the datagrid controls are instantiated, the validation controls have problems linking to the correct one.
I assume you could dynamically create the validation controls at run-time. However, I have not been successful at doing that.
Regardless, I code my own validation as part of the update of the datagrid.
Good luck :)
Looks like you guys figred it out. Unfortunately, there is no way to use validation controls when you use in-place editing on a datagrid.
The validation controls seem to be tied to a control on the form. Since techincally not all the datagrid controls are instantiated, the validation controls have problems linking to the correct one.
I assume you could dynamically create the validation controls at run-time. However, I have not been successful at doing that.
Regardless, I code my own validation as part of the update of the datagrid.
Good luck :)
dim intMax as integer = selUser.items.count
with selUser
for .SelectedIndex = 0 to (intMax - 1)
if .SelectedText = lbluserid.Text then
exit for ' Found match
end if
next