Getting column value from a gridview control

I have a grid view control and has one datakey and other columns. In my grid view I have an Edit Control and a Delete Control.

1) When I press Edit control, I am getting datakey value as expected. But I am not getting other column name i.e. pdf_filename

2) when I press Delete control, I should get the datakey value. But I am not getting

I am using c# 

Please help me

0
vmk
3/31/2008 4:01:14 AM
asp.net.presentation-controls 72751 articles. 3 followers. Follow

3 Replies
1240 Views

Similar Articles

[PageSpeed] 14

Hi vmk,

Could you be a littlemore elaborate?

like how have you bound your grid?

1.Columns autogenerated

2.Using TemplateFields

3.Using BoundFields.


Regards,
Naveen

Please remember to click Mark as Answer on the post that helps you, and to click Unmark as Answer if a marked post does not actually answer your question.
View Blog
0
naveenj
3/31/2008 4:08:57 AM

Thanks for your reply.

Its all autogenerated column

vmk

0
vmk
3/31/2008 4:32:52 AM

Hi vmk,

vmk:

1) When I press Edit control, I am getting datakey value as expected. But I am not getting other column name i.e. pdf_filename

2) when I press Delete control, I should get the datakey value. But I am not getting

You can set all of columns in DataKeyNames property of gridview.

And you can retrieve value using code below.

string id = this.GridView1.DataKeys[e.NewEditIndex].Values[0].ToString();
        string name = this.GridView1.DataKeys[e.NewEditIndex].Values[1].ToString();
        string age = this.GridView1.DataKeys[e.NewEditIndex].Values[2].ToString();

 Here is the sample,

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        string id = this.GridView1.DataKeys[e.NewEditIndex].Values[0].ToString();
        string name = this.GridView1.DataKeys[e.NewEditIndex].Values[1].ToString();
        string age = this.GridView1.DataKeys[e.NewEditIndex].Values[2].ToString();
    }
  
    <div>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
            DeleteCommand="DELETE FROM [Person] WHERE [id] = @id" InsertCommand="INSERT INTO [Person] ([id], [name], [age]) VALUES (@id, @name, @age)"
            SelectCommand="SELECT * FROM [Person]" UpdateCommand="UPDATE [Person] SET [name] = @name, [age] = @age WHERE [id] = @id">
            <DeleteParameters>
                <asp:Parameter Name="id" Type="Int64" />
            </DeleteParameters>
            <UpdateParameters>
                <asp:Parameter Name="name" Type="String" />
                <asp:Parameter Name="age" Type="Int64" />
                <asp:Parameter Name="id" Type="Int64" />
            </UpdateParameters>
            <InsertParameters>
                <asp:Parameter Name="id" Type="Int64" />
                <asp:Parameter Name="name" Type="String" />
                <asp:Parameter Name="age" Type="Int64" />
            </InsertParameters>
        </asp:SqlDataSource>
    
    </div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true" DataKeyNames="id,name,age"
            DataSourceID="SqlDataSource1" Width="254px" OnRowEditing="GridView1_RowEditing">
            <Columns>
                <asp:CommandField ShowEditButton="True" />
                </Columns>
        </asp:GridView>
 

Samu Zhang
Microsoft Online Community Support

Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question.
0
Samu
4/1/2008 4:07:49 AM
Reply:

Web resources about - Getting column value from a gridview control - asp.net.presentation-controls

GridView SelectedItem Binding Gotcha in WinRT
Last night when I was doing some WinRT hacking I was trying to setup databinding to the selected item in the GridView control and I ran into ...

CNW Group
Home Services Beyond the Wire About CNW Financials Webcasts Events My CNW All News Releases Advanced Search Gridview Latest News Releases All ...

CNW Group
... Services Beyond the Wire About CNW my CNW CNW Access CNW Directories Financials Webcasts Events my CNW All News Releases Advanced Search Gridview ...

CNW Group
Home Services Beyond the Wire About CNW Financials Webcasts Events My CNW All News Releases Add a Filter Gridview Latest News Releases All Today ...

New CmdLets of Powershell 2.0
New CmdLets of Powershell 2.0

Price Drop: Softbox
... objects- create product reflection- changing the background with something more interestingUse it as:- softbox- honeycomb softbox- gridview ...

Profile.aspx: ASP Alliance
.NET,ASP,ASP.NET,SQL,XML,HTML,ADO,ADO.NET,JavaScript,JScript,CSharp,VBScript and VB tutorials from AspAlliance.

Quick Answers - CodeProject
Questions - Free source code and tutorials for Software developers and Architects.; Updated: 25 Oct 2012

DotNetKicks.com - Upcoming stories
DotNetKicks.com is a community based news site edited by our members. It specialises in .NET development techniques, technologies and tools including ...

View Features - ExtendASP
Call 847-681-9827 or Request a Demo View Features Testimonials Pricing About Us Coming Soon ExtendASP exposes every feature, more than any other ...

Resources last updated: 1/21/2016 6:40:47 PM