How to get row data from DataGrid ASP:Button Click

D

DBLWizard

Howdy All,

I am trying to get the data from a row of a data grid in asp.net This
is a data grid with five columns (two of the hidden) as follows:



<asp:datagrid id="grdMatching" runat="server" Width="100%"
Visible="False" Height="160px" AutoGenerateColumns="False"
AllowSorting="True" onselectedindexchanged="GetRow">
<Columns>
<asp:ButtonColumn Text="Select" ButtonType="PushButton"
CommandName="Select"></asp:ButtonColumn>
<asp:TemplateColumn>
<HeaderTemplate>
Name
</HeaderTemplate>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "FullName") %>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn >
<HeaderTemplate>
Matches
</HeaderTemplate>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "Matches") %>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn Visible="False">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "Name") %>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn Visible="False">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "GivenName") %>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>

I am trying to use the ItemCommand event to get the data from the cells
as shown in the MSDN help but everything it comes back blank. Can
somebody tell me what I a am doing wrong? Here is the code I am using
in the ItemCommand event:

private void grdMatching_ItemCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string sConnection = "user
id=user;password=password;database=dbname;server=servername;Connect
Timeout=30";
SqlConnection myConn = new SqlConnection(sConnection);
SqlCommand myCommand;
SqlDataAdapter myDA = new SqlDataAdapter();
DataSet myDS;
string sName = "";
string sGivenName = "";
string sSql = "";

TableCell objCell = e.Item.Cells[3];
sName = objCell.Text;
objCell = e.Item.Cells[4];
sGivenName = objCell.Text;

if (sName.Length > 0 || sGivenName.Length > 0)
{
sSql = "Select top 100 InstrNum, FileType, FileDate, Book, Page,
Series, Type, Description, Series, Name, GivenName, RTrim([Name]) +
ISNULL(',' + RTrim([GivenName]), '') As [FullName] From NCLand Where
[Name] = '" + sName + "' AND [GivenName] = '" + sGivenName + " Order by
[Name], [GivenName]";
myCommand = new SqlCommand(sSql, myConn);
myDA = new SqlDataAdapter();
myDA.SelectCommand = myCommand;
myConn.Open();
myDS = new DataSet();
myDA.Fill(myDS, "SearchResults");
grdItems.DataSource = myDS.Tables["SearchResults"].DefaultView;
grdItems.DataBind();
grdItems.Visible = true;
}
}



This event fires but both cells come back with empty strings. How can
I get the data out of the cells?

Thanks

dbl
 
T

Teemu Keiski

Try placing Labels into the hidden columns and bind the values to them. When
checking for values, locate controls using FindControl method
(e.Item.FindControl...) and get the Text value from Label's Text property.

--
Teemu Keiski
ASP.NET MVP, Finland


DBLWizard said:
Howdy All,

I am trying to get the data from a row of a data grid in asp.net This
is a data grid with five columns (two of the hidden) as follows:



<asp:datagrid id="grdMatching" runat="server" Width="100%"
Visible="False" Height="160px" AutoGenerateColumns="False"
AllowSorting="True" onselectedindexchanged="GetRow">
<Columns>
<asp:ButtonColumn Text="Select" ButtonType="PushButton"
CommandName="Select"></asp:ButtonColumn>
<asp:TemplateColumn>
<HeaderTemplate>
Name
</HeaderTemplate>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "FullName") %>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn >
<HeaderTemplate>
Matches
</HeaderTemplate>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "Matches") %>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn Visible="False">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "Name") %>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn Visible="False">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "GivenName") %>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>

I am trying to use the ItemCommand event to get the data from the cells
as shown in the MSDN help but everything it comes back blank. Can
somebody tell me what I a am doing wrong? Here is the code I am using
in the ItemCommand event:

private void grdMatching_ItemCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string sConnection = "user
id=user;password=password;database=dbname;server=servername;Connect
Timeout=30";
SqlConnection myConn = new SqlConnection(sConnection);
SqlCommand myCommand;
SqlDataAdapter myDA = new SqlDataAdapter();
DataSet myDS;
string sName = "";
string sGivenName = "";
string sSql = "";

TableCell objCell = e.Item.Cells[3];
sName = objCell.Text;
objCell = e.Item.Cells[4];
sGivenName = objCell.Text;

if (sName.Length > 0 || sGivenName.Length > 0)
{
sSql = "Select top 100 InstrNum, FileType, FileDate, Book, Page,
Series, Type, Description, Series, Name, GivenName, RTrim([Name]) +
ISNULL(',' + RTrim([GivenName]), '') As [FullName] From NCLand Where
[Name] = '" + sName + "' AND [GivenName] = '" + sGivenName + " Order by
[Name], [GivenName]";
myCommand = new SqlCommand(sSql, myConn);
myDA = new SqlDataAdapter();
myDA.SelectCommand = myCommand;
myConn.Open();
myDS = new DataSet();
myDA.Fill(myDS, "SearchResults");
grdItems.DataSource = myDS.Tables["SearchResults"].DefaultView;
grdItems.DataBind();
grdItems.Visible = true;
}
}



This event fires but both cells come back with empty strings. How can
I get the data out of the cells?

Thanks

dbl
 
E

Eliyahu Goldin

Columns with Visible=false are not rendered to client. That's why you are
getting back empty strings. You should hide columns with css style
display:none.

Eliyahu

DBLWizard said:
Howdy All,

I am trying to get the data from a row of a data grid in asp.net This
is a data grid with five columns (two of the hidden) as follows:



<asp:datagrid id="grdMatching" runat="server" Width="100%"
Visible="False" Height="160px" AutoGenerateColumns="False"
AllowSorting="True" onselectedindexchanged="GetRow">
<Columns>
<asp:ButtonColumn Text="Select" ButtonType="PushButton"
CommandName="Select"></asp:ButtonColumn>
<asp:TemplateColumn>
<HeaderTemplate>
Name
</HeaderTemplate>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "FullName") %>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn >
<HeaderTemplate>
Matches
</HeaderTemplate>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "Matches") %>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn Visible="False">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "Name") %>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn Visible="False">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "GivenName") %>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>

I am trying to use the ItemCommand event to get the data from the cells
as shown in the MSDN help but everything it comes back blank. Can
somebody tell me what I a am doing wrong? Here is the code I am using
in the ItemCommand event:

private void grdMatching_ItemCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string sConnection = "user
id=user;password=password;database=dbname;server=servername;Connect
Timeout=30";
SqlConnection myConn = new SqlConnection(sConnection);
SqlCommand myCommand;
SqlDataAdapter myDA = new SqlDataAdapter();
DataSet myDS;
string sName = "";
string sGivenName = "";
string sSql = "";

TableCell objCell = e.Item.Cells[3];
sName = objCell.Text;
objCell = e.Item.Cells[4];
sGivenName = objCell.Text;

if (sName.Length > 0 || sGivenName.Length > 0)
{
sSql = "Select top 100 InstrNum, FileType, FileDate, Book, Page,
Series, Type, Description, Series, Name, GivenName, RTrim([Name]) +
ISNULL(',' + RTrim([GivenName]), '') As [FullName] From NCLand Where
[Name] = '" + sName + "' AND [GivenName] = '" + sGivenName + " Order by
[Name], [GivenName]";
myCommand = new SqlCommand(sSql, myConn);
myDA = new SqlDataAdapter();
myDA.SelectCommand = myCommand;
myConn.Open();
myDS = new DataSet();
myDA.Fill(myDS, "SearchResults");
grdItems.DataSource = myDS.Tables["SearchResults"].DefaultView;
grdItems.DataBind();
grdItems.Visible = true;
}
}



This event fires but both cells come back with empty strings. How can
I get the data out of the cells?

Thanks

dbl
 
D

DBLWizard

I saw that, have fixed it, and verified that the table cells are
actually there in the source but they code still returnes blanks. I
added code to check all the cells in the row and they all come back
blank!!!!

Any thoughts?

Thanks

dbl
 
D

DBLWizard

As I am reading more of the documentation it says that for "bound"
columns the values are in the Cell.Text. But I am using Template
Columns ... but it doesn't say how to get to the data for template
columns!!!! Is there a better way to do this?

Thanks

dbl
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,814
Latest member
SpicetreeDigital

Latest Threads

Top